var regfz =/I love (him|her|it)/;
console.log(regfz.test('I love him'));// true
console.log(regfz.test('I love her'));// true
console.log(regfz.test('I love it'));// true
console.log(regfz.test('I love them'));// false
console.log("----------------------");
// 将违禁词转换为等字数的星号是一个常见功能。比如文本是kid is a doubi,其中kid与doubi是违禁词,// 那么转换后应该为*** is a *****。我们可以这么写:var regdd =/(kid|doubi)/gvar strdd ='kid is a doubi'
strdd = strdd.replace(regdd,function(word){// word相当于正则匹配到的结果// console.log(word);//kid doubireturn word.replace(/./g,'*')})
console.log(strdd);//*** is a *****
console.log("----------------------");
var zfstr =' abc 234 abc abc'
console.log(zfstr.match(/(\s([a-z]+)\s)\d+\1\2/));// [" abc 234 abc abc", " abc ", "abc"]// 如果碰到类似/((kid) is (a (doubi)))/的嵌套分组,捕获的顺序是什么?来试试:var regqt =/((kid) is (a (doubi)))/var strqt ="kid is a doubi"
regqt.test(strqt)// true
console.log(RegExp.$1);// kid is a doubi
console.log(RegExp.$2);// kid
console.log(RegExp.$3);// a doubi
console.log(RegExp.$4);// doubi // 规则是以左括号出现的顺序进行捕获。
console.log("----------------------");