"hellohelloworld".match(/(hello){1,2}?/);
我在{1,2}后面加上?应该开启费贪婪模式了吧,但是为什么输出结果还是匹配了两次hello
如果你只要匹配一次,那你"hellohelloworld".match(/hello/)就好啦
"hellohelloworld".match(/hello/)
第一个是全局匹配第二个是括号匹配其实你的写法是对的 match方法返回的数组除了第一位之后都是匹配括号内的结果console.log("hellohelloworld".match(/(hello){1,2}?/))//[ "hello", "hello" ]console.log("hellohelloworld".match(/(hello){1,2}/))//[ "hellohello", "hello" ]
console.log("hellohelloworld".match(/(hello){1,2}?/))
console.log("hellohelloworld".match(/(hello){1,2}/))