正则原型test方法

1
2
3
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString);

或操作符|

1
2
3
let waldoIsHiding = "Somewhere Waldo is hiding in this text.";
let waldoRegex = /Waldo/;
let result = waldoRegex.test(waldoIsHiding);

正则标志

字符串原型方法:match

通配符:.(点)

字符集匹配:[]

1
2
let vowelRegex = /[aeiou]/gi; 
let result = quoteSample.match(vowelRegex);

字符集 连字符:- 字符范围

1
2
3
let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/gi;
let result = quoteSample.match(alphabetRegex);

否定字符集:^

+*?^$

贪婪匹配 & 惰性匹配

字符集

匹配的数量上下限

先行断言:在字符串中向前查找的匹配模式。

例子:

捕获组:重用子字符串

字符串原型方法replace:捕获组 搜索/替换