实战,参数可以对照上面的,代码一定要敲
var str = 'abc123cba456aaa789'; var reg = /d+/g; console.log(reg.test(str)) //str满足匹配表达式返回true str2 = 'asdsadasd' console.log(reg.test(str2)) //str2不满足匹配表达式返回flase console.log(reg.exec(str)) //返回数组['123','input':'abc123cba456aaa789'] console.log(reg.lastIndex) //返回匹配的位置0 console.log(reg.exec(str)) //返回数组['456','input':'abc123cba456aaa789'] console.log(reg.lastIndex) //返回匹配的位置1 var reg2 = /w(d+)/g //下面返回数组['c123','123','input':'abc123cba456aaa789'],包含分组内容 console.log(reg.exec(str)) console.log(str.match(reg)) //返回数组['123','456','789'] console.log(str.replace(reg,"Q")) //返回'abcQcbaQaaaQ' console.log(str.replace(reg,function(){ console.log(arguments) return "Q" //返回['123',3,'abc123cba456aaa789']和 //['456',9,'abc123cba456aaa789']和 //['789',15,'abc123cba456aaa789'] })) //输出'abcQcbaQaaaQ'