在
g正则表达式(简称 全球 )说,搜索整个字符串,而不是只要找到第一次出现。这匹配
is两次:
var temp = "This is a string.";var count = (temp.match(/is/g) || []).length;console.log(count);
并且,如果没有匹配项,则返回
0:
var temp = "Hello World!";var count = (temp.match(/is/g) || []).length;console.log(count);



