var str = "123456abc1abc";var re = /(\d+)(\D\D)/g; //结果是["123456ab", "1ab"]// var re = /(\d+?)(\D\D)/g; //结果是["123456ab", "1ab"] 这里非贪婪不应该是6ab 和 1ab吗console.log(str.match(re))
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
可以这么理解,没人要的东西你多要,不能叫做贪婪。
再举个栗子:
var str='1234abc56efg';
var regx1=/(\d+)(\d+)(\D\D)/g;
var regx2=/(\d+?)(\d+)(\D\D)/g;
console.log(regx1.exec(str)); // ["1234ab", "123", "4", "ab", index: 0, input: "1234abc56efg"]
console.log(regx2.exec(str)); // ["1234ab", "1", "234", "ab", index: 0, input: "1234abc56efg"]
注意匹配 $1 和匹配 $2 的值的区别
添加回答
举报
0/150
提交
取消