js正则表达式

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))


忽然笑
浏览 411回答 1
1回答

长风秋雁

可以这么理解,没人要的东西你多要,不能叫做贪婪。再举个栗子: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 的值的区别
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript