##正则
目录
- 转义
- 元字符
- 量词
- 字界符
- 修饰符
- 正则方法之字符串
- js 正则表达式(.+)和(.+?)的区别
- 具名组匹配
- 如果存在多个同名参数,则返回数组
- 正则对象构造函数的全局属性 $1 - $9 来获取
- 常用
转义
\d:[0-9],数字 digit
\s:[\t\v\n\r\f],空白符,空格,水平制表符(\t),垂直制表符(\v),换行符(\n),回车符(\r),换页符(\f), space character
\w:[0-9a-zA-Z],表示数字大小写字母和下划线 word
\D:[^0-9],非数字
\S:[^\t\v\n\r\f],非空白符
\W:[^0-9a-zA-Z],非单词字符
元字符
. : 匹配除了换行符之外的任何单个字符
\ : 将下一个字符标记为特殊字符,例如, 'n' 匹配字符 'n'。'\n' 匹配 换行符。序列 '\\' 匹配 "\",而 '\(' 则匹配 "("。
| : 逻辑或操作符
[ ] :定义一个字符集合,匹配字符集合中的一个字符,在字符集合里面像.,\这些字符都表示其本身
- :定义一个区间,例如[A-Z],其首尾字符在 ASCII 字符集里面
量词
{m,n} :匹配前面一个字符至少 m 次至多 n 次重复,还有{m}表示匹配 m 次,{m,}表示至少 m 次
+ : 匹配前面一个表达式一次或者多次,相当于 {1,},记忆方式追加(+),起码得有一次
* : 匹配前面一个表达式零次或者多次,相当于 {0,},记忆方式乘法(*),可以一次都没有
? : 单独使用匹配前面一个表达式零次或者一次,相当于 {0,1},有(1) 或者没有(1),如果跟在任何量词*,+,?,{}后面的时候将会使量词变为非贪婪模式(尽量匹配少的字符),默认是使用贪婪模式。比如对 "123abc" 应用 /\d+/ 将会返回 "123",如果使用 /\d+?/,那么就只会匹配到 "1"。
字界符
i :不区分大小写 ignore
g : 全局搜索
m :多行搜索
修饰符
^ : 单独使用匹配表达式的开始
\$ : 匹配表达式的结束
正则方法之字符串
search match replace split
- search 测试匹配的 String 方法,它返回匹配到的位置索引,或者在失败时返回-1。
js
let str = "testabcdef";
let reg = /a/i;
str.search(reg); // 4
- match 查找匹配的 String 方法,它返回一个数组或者在未匹配到时返回 null。
js
let str1 = "alis sha343nshui sa3ange sha5gua dfs6";
reg = /\d+/gi;
console.log(str1.match(reg)); // ["343", "3", "5", "6"]
- replace 执行查找匹配的 String 方法,并且使用替换字符串替换掉匹配到的子字符串。
js
let str = "ABc, bcd, fas";
str.replace(/a/gi, "*"); // '*Bc,bcd,f*s'
- split 一个使用正则表达式或者一个固定字符串分隔一个字符串,并将分隔后的子字符串存储到数组中的 String 方法。
js
let str = "AAAAA##BBBBB##CCCC##DDD";
let newStr = str
.split(/##/g)
.map((item) => {
return `<p>${item}</p>`;
})
.join(""); // <p>AAAAA</p><p>BBBBB</p><p>CCCC</p><p>DDD</p>
- vue 中使用
js
let str = 'aaaa{{value1}}aaaddg{{value2}}fhjdr{{value3}}'
let reg = /{{([^{][\s\S]+?|[\d]+?[^}])}}/g
let arr = .match(reg)
arr = arr.map(e => e.replace(/{{|}}/g, ''))
console.log(arr) // ["value1", "value2", "value3"]
js 正则表达式(.+)和(.+?)的区别
js
var str = 'aaa<div style="font-color:red;">123456</div>bbb';
// 惰性匹配
str.match(/<.+?>/); // <div style="font-color:red;">
// 贪婪匹配 默认贪婪
str.match(/<.+>/); // <div style="font-color:red;">123456</div>
// 惰性匹配 实例
var regex = /\d{2,5}?/g;
var string = "123 1234 12345 123456";
console.log(string.match(regex)); // ["12", "12", "34", "12", "34", "12", "34", "56"]
具名组匹配
js
const RE_DATE = /(\d{4})-(\d{2})-(\d{2})/;
const matchObj = RE_DATE.exec("1999-12-31");
const year = matchObj[1]; // 1999
const month = matchObj[2]; // 12
const day = matchObj[3]; // 31
js
// ES2018 引入了具名组匹配,允许为每一个组匹配指定一个名字,既便于阅读代码,又便于引用。
// 模式的头部添加“问号 + 尖括号 + 组名”(?<year>)。
exec方法返回结果的groups属性上引用该组名。同时,数字序号(matchObj[1])依然有效。
const RE_DATE = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const matchObj = RE_DATE.exec('1999-12-31');
const year = matchObj.groups.year; // 1999
const month = matchObj.groups.month; // 12
const day = matchObj.groups.day; // 31
如果存在多个同名参数,则返回数组
js
function getUrlParam3(sUrl, sKey) {
var resObj = {};
var reg = /(\w+)=(\w+)/g;
while (reg.exec(sUrl)) {
resObj[RegExp.$1]
? (resObj[RegExp.$1] = [].concat(resObj[RegExp.$1], RegExp.$2))
: (resObj[RegExp.$1] = RegExp.$2);
}
if (sKey) {
return resObj[sKey] ? resObj[sKey] : "";
}
return resObj;
}
正则对象构造函数的全局属性 $1 - $9 来获取
js
var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-08-09";
regex.test(string); // 正则操作即可,例如
//regex.exec(string);
//string.match(regex);
console.log(RegExp.$1); // "2017"
console.log(RegExp.$2); // "08"
console.log(RegExp.$3); // "09"
js
var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-08-09";
var result = string.replace(regex, "$2/$3/$1");
console.log(result); // "08/09/2017"
// 等价
var result = string.replace(regex, function () {
return RegExp.$2 + "/" + RegExp.$3 + "/" + RegExp.$1;
});
console.log(result); // "08/09/2017"
// 等价
var regex = /(\d{4})-(\d{2})-(\d{2})/;
var string = "2017-08-09";
var result = string.replace(regex, function (match, year, month, day) {
return month + "/" + day + "/" + year;
});
console.log(result); // "08/09/2017"
常用
js
var reg = /(?:<)([^<>]+)(?:>)/g; // 匹配html字符串中的所有标签
var hanzi = /[\u4e00-\u9fa5]/; // 匹配汉字
var doubleByteCharacter = /[^\x00-\xff]/; // 匹配双字节字符(包括汉字在内)
java
/******************** 正则相关常量 ********************/
/**
* 正则:手机号(简单)
*/
public static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
/**
* 正则:手机号(精确)
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p>
* <p>联通:130、131、132、145、155、156、175、176、185、186</p>
* <p>电信:133、153、173、177、180、181、189</p>
* <p>全球星:1349</p>
* <p>虚拟运营商:170</p>
*/
public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
/**
* 正则:电话号码
*/
public static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";
/**
* 正则:身份证号码15位
*/
public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
/**
* 正则:身份证号码18位
*/
public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
/**
* 正则:邮箱
*/
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 正则:URL
*/
public static final String REGEX_URL = "[a-zA-z]+://[^\\s]*";
/**
* 正则:汉字
*/
public static final String REGEX_ZH = "^[\\u4e00-\\u9fa5]+$";
/**
* 正则:用户名,取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位
*/
public static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?<!_)$";
/**
* 正则:yyyy-MM-dd格式的日期校验,已考虑平闰年
*/
public static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
/**
* 正则:IP地址
*/
public static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
/************** 以下摘自http://tool.oschina.net/regex **************/
/**
* 正则:双字节字符(包括汉字在内)
*/
public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\\x00-\\xff]";
/**
* 正则:空白行
*/
public static final String REGEX_BLANK_LINE = "\\n\\s*\\r";
/**
* 正则:QQ号
*/
public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}";
/**
* 正则:中国邮政编码
*/
public static final String REGEX_ZIP_CODE = "[1-9]\\d{5}(?!\\d)";
/**
* 正则:正整数
*/
public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\\d*$";
/**
* 正则:负整数
*/
public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\\d*$";
/**
* 正则:整数
*/
public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";
/**
* 正则:非负整数(正整数 + 0)
*/
public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\\d*|0$";
/**
* 正则:非正整数(负整数 + 0)
*/
public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\\d*|0$";
/**
* 正则:正浮点数
*/
public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$";
/**
* 正则:负浮点数
*/
public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$";
/************** If u want more please visit http://toutiao.com/i6231678548520731137/ **************/
// 手机号➕✨
str.replace(/(\d{3})\d*(\d{4})/, '$1****$2');
var regx = /^[\w]?[\w\s.,]+[\w]$/ig // 只能输入英文 数字逗号空格
// str="我们都是阿斯顿发好孩子"
// str.replace(/([a-zA-Z0-9\u4E00-\u9FA5]{1})[a-zA-Z0-9\u4E00-\u9FA5]*([a-zA-Z0-9\u4E00-\u9FA5]{1})/, '$1****$2');
// "我****子"