1.简介
使用 * + 等匹配重复模式时,有时可以匹配的数量是不确定的
贪婪模式是尽可能多的匹配,懒惰模式是尽可能少的匹配
2.使用
默认情况是贪婪模式,懒惰模式需要添加一个?,此时?不代表0或1次,而是修饰前面的* 或 +
new RegExp(/[1-9]*/g).exec('123456')
[ '123456', index: 0, input: '123456', groups: undefined ]
new RegExp(/[1-9]*?/g).exec('123456')
[ '', index: 0, input: '123456', groups: undefined ]
new RegExp(/[1-9]+/g).exec('123456')
[ '123456', index: 0, input: '123456', groups: undefined ]
new RegExp(/[1-9]+?/g).exec('123456')
[ '1', index: 0, input: '123456', groups: undefined ]
可以看到默认的贪婪模式都是匹配最大数量
而懒惰模式匹配了最小数量