正则表达式02
2020-12-09 本文已影响0人
正直走
-
点符号表示匹配除了换行符外的任意字符
let pattern = /g..gle/; let str = 'g12gle'; alert(pattern.test(str));
-
o*,表示0个,1个,或者多个o
let pattern = /go*gle/; let str = 'gbbbgle'; alert(pattern.test(str));
-
o+,表示1个,或者多个
let pattern = /go+gle/; let str = 'ggle'; alert(pattern.test(str));
-
o?,表示1个,或者0个
let pattern = /go?gle/; let str = 'google'; alert(pattern.test(str));
-
.?,表示1个,或者0个的任意字符
let pattern = /g.?gle/; let str = 'gbbgle'; alert(pattern.test(str));
-
o{2,4}表示匹配o2-4次,包含2和4
let pattern = /go{2,4}gle/; let str = 'google'; alert(pattern.test(str));
-
o{3},表示只能限定为3个
let pattern = /go{3}gle/; let str = 'goooogle'; alert(pattern.test(str));
-
o{3,},表示为3个或3个以上
let pattern = /go{3,}gle/; let str = 'goooooooooooooooooooogle'; alert(pattern.test(str));
-
[a-z]表示26个小写字母,任意一个都可以匹配
let pattern = /[a-z]oogle/; let str = '6oogle'; alert(pattern.test(str));
-
[A-Z]表示26个大写字母,任意一个都可以匹配
let pattern = /[A-Z]oogle/;
let str = 'Google';
alert(pattern.test(str));
- [0-9]*,表示0次,一次,或者多次
let pattern = /[0-9]*oogle/;
let str = '44444oogle';
alert(pattern.test(str));
- [a-zA-Z0-9]表示匹配大小写的a-zA-Z0-9
let pattern = /[a-zA-Z0-9]oogle/;
let str = 'oogle';
alert(pattern.test(str));
- [^0-9]表示非0-9的任意字符
let pattern = /[^a-zA-Z0-9]oogle/;
let str = '_oogle';
alert(pattern.test(str));
- 这个^符号,是加在/后面的而不是[]里面的
let pattern = /^[0-9]oogle/;
let str = '444oogle';
alert(pattern.test(str));
- +号,可以匹配一个或者多个
let pattern = /^[0-9]+oogle/;
let str = '444oogle';
alert(pattern.test(str));
- \w表示[a-zA-Z_]
let pattern = /\woogle/;
let str = '-oogle';
alert(pattern.test(str));
- \W表示[^a-zA-Z_]
let pattern = /\Woogle/;
let str = '-oogle';
alert(pattern.test(str));
- \d表示[0-9]
let pattern = /\doogle/;
let str = 'aoogle';
alert(pattern.test(str));
- \D表示[^0-9]
let pattern = /\Doogle/;
let str = '1oogle';
alert(pattern.test(str));
- ^强制首匹配,$强制尾匹配
let pattern = /^[a-z]oogl[0-9]$/;
let str = 'googl5';
alert(pattern.test(str));
```
21. \S匹配非空白字符,\s匹配空白字符、空格、制表符和换行符
```
let pattern = /\S\.vue$/;
let str = 'index.vue';
alert(pattern.test(str));