网站买源代码,网络推广都有哪些渠道,网页制作与设计的总结,抓取工具把对手网站的长尾词一、定义的不同RegExp.prototype.test()RegExp.prototype.exec()String.prototype.match()从MDN的定义可以看出#xff0c;test和exec是正则实例的API#xff0c;match是String的#xff0c;这一点决定了调用方式的不同。二、应用场景的不同如果只是想要判断正则表达式和字符…一、定义的不同RegExp.prototype.test()RegExp.prototype.exec()String.prototype.match()从MDN的定义可以看出test和exec是正则实例的APImatch是String的这一点决定了调用方式的不同。二、应用场景的不同如果只是想要判断正则表达式和字符串是否匹配用test是最简单的。const bool /^hello/.test(helloworld) // true
如果你不只是想要知道是否匹配还想知道匹配的结果那么就可以用match。const arr helloworld.match(/^hello/)
// [hello, index:0, input: helloworld, group: undefined]如果你不仅想知道匹配结果还想遍历匹配结果那就可以用exec。const reg /foo*/g;
const str table football foosball;
let res;
while ((res reg.exec(str)) ! null) {console.log(, res);
}执行结果如下这里需要注意的是如果要遍历正则结果正则表达式一定要加上全局标识g。