0.正则匹配规则
- 字符串从左到右,依次先匹配多再匹配少,如果一旦匹配上就不会回头匹配
- 贪婪匹配原则:能匹配多个,就不匹配少个
1.创建正则表达式的三种方式
1.1字面量的方式
let reg = /test/;
let str = 'this is a test,test is very good,you can use test to work!'
let bool = reg.test(str);
console.log(bool);
1.2系统构造函数的方式
let reg = RegExp('test');
let str = 'this is a test,test is very good,you can use test to work!'
let bool = reg.test(str);
console.log(bool);
1.3new
系统构造函数的方式
let reg =new RegExp('test');
let str = 'this is a test,test is very good,you can use test to work!'
let bool = reg.test(str);
console.log(bool);
2.修饰符
2.1i
忽略大小写
2.1.1不使用i
let reg = /TeSt/;
let str = 'this is a test,test is very good,you can use test to work!'
let bool = reg.test(str);
console.log(bool);
2.1.2使用i
let reg = /TeSt/i;
let str = 'this is a test,test is very good,you can use test to work!'
let bool = reg.test(str);
console.log(bool);
2.2g
全局匹配
2.2.1不使用g
let reg = /test/;
let str = 'this is a test,test is very good,you can use test to work!'
let arr = str.match(reg);
console.log(arr);
2.2.2使用g
let reg = /test/g;
let str = 'this is a test,test is very good,you can use test to work!'
let arr = str.match(reg);
console.log(arr);
2.3m
多行匹配
2.3.1不使用m
let reg = /^test/;
let str = `this is a test,
test is very good,you can use
test to work!`
let arr = str.match(reg);
console.log(arr);
2.3.2使用m
let reg = /^test/m;
let str = `this is a test,
test is very good,you can use
test to work!`
let arr = str.match(reg);
console.log(arr);
2.4s
增强.能匹配到\n
2.4.1不使用s
let reg = /./;
let str = '\n';
let bool=reg.test(str);
console.log(bool);
2.4.2使用s
let reg = /./s;
let str = '\n';
let bool=reg.test(str);
console.log(bool);
3.原子
3.1\d
和\D
3.1.1\d
匹配任意数字[0-9]
let reg = /\d/g;
let str = 'alskdjfa3sd54fasdfas231ef65as4df\n——_';
let arr=str.match(reg);
console.log(arr);
3.1.2\D
匹配任意非数字[^0-9]
let reg = /\D/g;
let str = 'alskdjfa3sd54fasdfas231ef65as4df\n——_';
let arr=str.match(reg);
console.log(arr);
3.1.3匹配任意字符
let reg = /[\d|\D]/g;
let str = 'alskdjfa3sd54fasdfas231ef65as4df\n——_';
let arr=str.match(reg);
console.log(arr);
3.2\w
和\W
3.2.1\w
匹配任意英文字母数字或者下划线[0-9A-z_]
let reg = /\w/g;
let str = 'alskdjfa3sd54fasdfas231ef65as4df\n——_';
let arr=str.match(reg);
console.log(arr);
3.2.2\W
匹配非任意英文字母数字或者下划线[^0-9A-z_]
let reg = /\W/g;
let str = 'alskdjfa3sd54fasdfas231ef65as4df\n——_';
let arr=str.match(reg);
console.log(arr);
3.2.3匹配任意字符
let reg = /[\w\W]/g;
let str = `alsk djfa3s d54f\rasdf\tas231ef6
5as4df\n——_`;
let arr=str.match(reg);
console.log(arr);
3.4\s
和\S
3.4.1\s
匹配任意空白字符[\n\t\r]
let reg = /\s/g;
let str = `alsk djfa3s d54f\rasdf\tas231ef6
5as4df\n——_`;
let arr=str.match(reg);
console.log(arr);
3.4.2\S
匹配任意非空白字符[^\n\t\r]
let reg = /\S/g;
let str = `alsk djfa3s d54f\rasdf\tas231ef6
5as4df\n——_`;
let arr=str.match(reg);
console.log(arr);
3.4.3匹配任意字符
let reg = /[\s\S]/g;
let str = `alsk djfa3s d54f\rasdf\tas231ef6
5as4df\n——_`;
let arr=str.match(reg);
console.log(arr);
3.5\b
单词边界和\B
非单词边界
let reg = /\btest\b/g;
let str = `this is test,test is very good,you can use test to work 1test test2 1test2`;
let arr=str.match(reg);
console.log(arr);
4.元字符
4.1.
匹配除了换行符\n
的任意字符[所有字符]
let reg = /./g;
let str = `this is test,test is very good,\nyou can use test to work 1test test2 1test2 `;
let arr = str.match(reg);
console.log(arr);
4.2\
将其带有特殊含义的字符去掉后匹配
let reg = /(\.|\\)/g;
let str = `\\this.`;
let arr = str.match(reg);
console.log(arr);
4.3|
等同于js
中的逻辑或
let reg = /ab|sdf/g;
let str = `abcafdssdf`;
let arr = str.match(reg);
console.log(arr);
4.3.1[]
只匹配其中的一个原子
let reg = /[abc][def]/g;
let str = `abcdefhig`;
let arr = str.match(reg);
console.log(arr);
4.3.2[^]
只匹配除了匹配 原子的所有原子
let reg = /[^abc][def]/g;
let str = `abcd1fefhig`;
let arr = str.match(reg);
console.log(arr);
5.边界匹配
5.1^
开头
let reg = /^[abc]\w/g;
let str = `asdkjfl bsldkf calskd4sdwsa`;
let arr = str.match(reg);
console.log(arr);
5.2$
结尾
let reg = /\w[abc]$/g;
let str = `asdkjfl bsldkf calskd4sdwsa`;
let arr = str.match(reg);
console.log(arr);
6.量词
6.1*
重复0次或者多次{0,}
let reg = /abc*/g;
let str = 'abcabccc';
let arr = str.match(reg);
console.log(arr);
6.2+
重复1次或者多次{1,}
let reg = /bc+/g;
let str = 'abcdabcd';
let arr = str.match(reg);
console.log(arr);
6.3?
重复0次或者1次{0,1}
let reg = /bc?/g;
let str = 'abcdabcd';
let arr = str.match(reg);
console.log(arr);
6.4{n}
重复n次
let reg = /^123\d{8}/g;
let str = '12345678912';
let bool = reg.test(str);
console.log(bool);
let arr = str.match(reg);
console.log(arr);
6.5{n,}
重复n次或者更多次
let reg = /^123\d{20,}/g;
let str = '12345678912';
let bool = reg.test(str);
console.log(bool);
let arr = str.match(reg);
console.log(arr);
6.6{n,m}
重复n次到m次
let reg = /^123\d{6,12}/g;
let str = '12345678912';
let bool = reg.test(str);
console.log(bool);
let arr = str.match(reg);
console.log(arr);
7.子表达式反向引用
let reg = /(\w)\1\1/g;
let str = 'aaaaaaabbbbbdssdfsdfdaasssaaaaddf111223sddfadsssss';
let arr = str.match(reg);
console.log(arr);
let reg = /(\w)\1(\w)\2/g;
let str = 'aaaaaaabbbbbdssdfsdfdaasssaaaaddf111223sddfadsssss';
let arr = str.match(reg);
console.log(arr);
8.正向预查
8.1?=n
匹配任何其后紧跟着字符串n的字符串(正向预查)
let reg = /a(?=b)/g;
let str = 'abcdabcd';
let bool = reg.test(str);
console.log(bool);
let arr = str.match(reg);
console.log(arr);
8.2?!n
匹配非任何其后紧跟着字符串n的字符串()
let reg = /a(?!b)/g;
let str = 'abcdabcdansdfsdf';
let arr = str.match(reg);
console.log(arr);
8.3?:b
不匹配该分组
let reg = /(?:b)(c)/;
let str = 'abcabcabc';
console.log(str.match(reg));
9.贪婪模式和非贪婪模式
9.1贪婪模式(正则匹配默认都是贪婪模式)
let reg = /{{.*}}/gs;
let str = 'abcd{{efg}}abcd{{xyz}}';
let arr = str.match(reg);
console.log(arr);
9.2非贪婪模式
let reg = /{{.*?}}/gs;
let str = 'abcd{{efg}}abcd{{xyz}}';
let arr = str.match(reg);
console.log(arr);
10.正则表达式的几个方法
10.1replace
10.1.1基本使用
let reg = /\w??/gs;
let str = `两匹马各拉一辆大车。前面的一匹马奋力前行,走的很快,而后面的一匹马磨磨蹭蹭,还常常停下来休息。主人觉得也许是后面的马力气小,就一次又一次地把后面那辆车上的货物挪到前面的车上去。等到车上的东西都搬完了,后面那匹马便轻快地前进,并且嘲笑前面那匹马说:“你力气大就好好流汗吧,你越是努力干,人家给你的任务就越重。”
来到旅店休息的时候,主人说:“既然只用一匹马拉车,我养两匹马干吗?不如把另一匹宰掉,总还能拿到一张皮吧。”于是,他便把那匹爱偷懒的马送到屠宰场了。`;
let newStr=str.replace(/匹马/g,'只牛').replace(/匹/g,'只').replace(/马/g,'牛');
console.log(newStr);
10.1.2aabbccdd=>bbaaddcc
10.1.2.1非命名捕获
10.1.2.1.1方法一
let str = 'aabbccdd';
let reg = /(\w)\1(\w)\2/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$2$2$1$1');
console.log(newStr);
10.1.2.1.2方法二
let str = 'aabbccdd';
let reg = /(\w)\1(\w)\2/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function ($, $1, $2) {
console.log($, $1, $2);
return $2 + $2 + $1 + $1;
});
console.log(newStr);
10.1.2.2命名捕获
10.1.2.2.1方法一
let str = 'aabbccdd';
let reg = /(?<key1>\w)\1(?<key2>\w)\2/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$<key2>$<key2>$<key1>$<key1>');
console.log(newStr);
10.1.2.2.2方法二
let str = 'aabbccdd';
let reg = /(?<key1>\w)\1(?<key2>\w)\2/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function (...arr) {
console.log(arr[0]);
let json = arr[arr.length - 1];
console.log(json);
return json.key2 + json.key2 + json.key1 + json.key1;
});
console.log(newStr);
10.1.3js-plus-plus=>jsPlusPlus
10.1.3.1非命名捕获
let str = 'js-plus-plus';
let reg = /-(\w)/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function($,$1){
console.log($,$1);
return $1.toLocaleUpperCase();
});
console.log(newStr);
10.1.3.2命名捕获
let str = 'js-plus-plus';
let reg = /-(?<key>\w)/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function(...arr){
console.log(arr[0]);
let json=arr[arr.length-1];
return json.key.toLocaleUpperCase();
});
console.log(newStr);
10.1.4jsPlusPlus=>js_plus_plus
10.1.4.1非命名捕获
let str = 'jsPlusPlus';
let reg = /([A-Z])/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function($,$1){
return '_'+$1.toLocaleLowerCase();
});
console.log(newStr);
10.1.4.2命名捕获
let str = 'jsPlusPlus';
let reg = /(?<key>[A-Z])/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function (...arr) {
console.log(arr[0]);
let json = arr[arr.length - 1];
return '_' + json.key.toLocaleLowerCase();
});
console.log(newStr);
10.1.5xxyyzz=>XxYyZz
10.1.5.1非命名捕获
let str = 'xxyyzz';
let reg = /(\w)\1/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function ($, $1) {
console.log($, $1);
return $1.toLocaleUpperCase() + $1;
});
console.log(newStr);
10.1.5.2命名捕获
let str = 'xxyyzz';
let reg = /(?<key>\w)\1/g;
console.log(str.match(reg));
let newStr = str.replace(reg, function (...arr) {
console.log(arr[0]);
let json = arr[arr.length - 1];
return json.key.toLocaleUpperCase() + json.key;
});
console.log(newStr);
10.1.6aabbcc=>a$b$c$
10.1.6.1非命名捕获
let str = 'aabbcc';
let reg = /(\w)\1(\w)\2(\w)\3/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$1$$$2$$$3$');
console.log(newStr);
10.1.6.2命名捕获
let str = 'aabbcc';
let reg = /(?<key1>\w)\1(?<key2>\w)\2(?<key3>\w)\3/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$<key1>$$$<key2>$$$<key3>$');
console.log(newStr);
10.1.7aaaaaaaabbbbbbbbbbcccccccc=>abc
10.1.7.1非命名捕获
let str = 'aaaaaaaabbbbbbbbbbcccccccc';
let reg = /(\w)\1*/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$1');
console.log(newStr);
10.1.7.2命名捕获
let str = 'aaaaaaaabbbbbbbbbbcccccccc';
let reg = /(?<key>\w)\1*/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '$<key>');
console.log(newStr);
10.1.8数字变成账户余额模式
let str = '100100402000011';
let reg = /(?=(\B)(\d{3})+$)/g;
console.log(str.match(reg));
let newStr = str.replace(reg, '.');
console.log(newStr);
10.1.9字符串替换
let reg = /{{(.+?)}}/gs;
let str = `
<h2>{{title}}</h2>
<p>{{describe}}</p>
`;
console.log(str.match(reg));
let newStr = str.replace(reg, function (node, key) {
console.log(node, key);
return {
title: '新闻',
describe: '这是一条有味道的新闻'
}[key]
});
console.log(newStr);
11.验证密码
let inputDOM = document.querySelector('input');
inputDOM.addEventListener('keyup', function () {
let reg = /^(?=.*\d)(?=.*[A-z])[0-9A-z]{6,18}$/g;
let inputValue = this.value;
console.log(reg.test(inputValue));
}, false);
12.校验身份证
let inputDOM = document.querySelector('input');
inputDOM.addEventListener('keyup', function () {
let reg = /^[1-9]\d{5}((18|19|20)\d{2})(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])(\d{3})[0-9Xx]$/g;
let inputValue = this.value;
console.log(reg.test(inputValue));
}, false)
13.校验日期
/(\d{4})([-/\.])(0[1-9]|1[0-2])\2([12][0-9]|0[1-9]|3[01])/g
14.匹配微信号
/^[A-z][A-z0-9_-]{5,19}$/g
15.匹配车牌号
/^[京津沪渝冀豫云辽黑湘皖新鲁苏浙晋赣鄂桂甘蒙陕吉闽贵粤青藏川宁琼][A-Z][A-Z0-9]{5}$/g;