<分区>
我有一个变量,
var inp = "B123213";
现在当我应用下面的正则表达式时,
var rg = /\d+/;
并执行String
匹配
操作,
console.log(inp.match(rg));
我得到的输出为 123213
,表示表达式是贪心的。但是当我使用 *
而不是 +
时,表达式不会返回匹配项。请帮助我理解为什么返回空匹配项。它不应该返回相同的结果吗。
由于文档中提到了 ?
,
If used immediately after any of the quantifiers *, +, ?, or {}, makes the quantifier non-greedy (matching the fewest possible characters), as opposed to the default, which is greedy (matching as many characters as possible). For example, applying /\d+/ to "123abc" matches "123". But applying /\d+?/ to that same string matches only the "1".
我希望仅在应用 /\d*?/
时出现空匹配。