exec() 方法在一个指定字符串中执行一个搜索匹配。返回一个结果数组或 null。数组
若是你只是为了判断是否匹配(true或 false),可使用 RegExp.test() 方法,或者 String.search() 方法。
var regex1 = RegExp('foo*','g'); var str1 = 'footfootsball'; var array1; while ((array1 = regex1.exec(str1)) !== null) { console.log(array1) console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`); // expected output: "Found foo. Next starts at 9." // expected output: "Found foo. Next starts at 19." }