new RegExp(pattern, attributes);
i 修饰符用于执行对大小写不敏感的匹配。找到第一个后就停止
输出结果为W3School
g 修饰符用于执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。
输出结果为is,is
输出结果h,a,h,e,e
单词字符包括:a-z、A-Z、0-9,以及下划线。
结果为
输出结果为100 1000 10000,包含有三个\d即三个数字,如果改为\d{4,}的话就是1000和10000,把数拆分成一个一个数字然后计算其个数。
对其后紧跟 “all” 的 “is” 进行搜索,/后加个g就是全局搜索
var str = "The rain in Spain stays mainly in the plain";
var patt1 = new RegExp("ain", "g");
for(i = 0; i < 7; i++)
{
patt1.test(str)
document.write("ain found. index now at: " + patt1.lastIndex);
document.write("<br />");
}
输出结果为
ain found. index now at: 8
ain found. index now at: 17
ain found. index now at: 28
ain found. index now at: 43
ain found. index now at: 0
ain found. index now at: 8
ain found. index now at: 17
var str="Every man in the world! Every woman on earth!";
patt=/man/g;
str2=str.replace(patt,"person");
document.write(str2+"<br />");
patt=/(wo)?man/g;
patt.compile(patt);
str2=str.replace(patt,"person");
document.write(str2);
Every person in the world! Every woperson on earth!
Every person in the world! Every person on earth!
search返回匹配的子串的第一个值,如果无匹配返回-1