- 精准匹配
const a = 'abhello'
const isHave = a.test('hello')
- 横向模糊(数量)
const testRule = /ab{3,5}c/g
const a = 'abbbc abbbbbc abbbbbbbbbc'
console.log(a.match(testRulr))
- 纵向模糊
const testRule = /1[345]8/g
const a = '138 123 148'
console.error(a.match(testRule))
const testRule = [1234567abcd]
const testRule = [1\-7a\-d]
- 量词
{m,} 出现m次数以上
{m} m次就可以
{m,n} m-n次
+ 至少一次
? 有没有
* 几次都可以
var regex = /\d{2,5}?/g;
var string = "123 1234 12345 123456";
console.log( string.match(regex) );
- 常见的简写形式
^ 字符组的第一位放`^`(脱字符),表示求反的概念。[^0-9]不匹配0-9
\d [0\-9] 表示每一个数字
\D [^0\-9]
\w [0-\9a\-zA\-Z] 表示匹配字符
\W [^0-9a-zA-Z]表示匹配非字符
\s 表示匹配空格
\S 表示匹配非空格
. 表示匹配几乎任意字符
[\d\D] [\s\S] [\w\W] [^] 表示匹配任意字符
- 分支匹配 |
const testRule = /good | goodbye/g
const str = 'good study,goodbye'
console.log(str.match(testRule))