I have a variable,
var inp = "B123213";
Now when i apply the below regex,
var rg = /\d+/;
and perform String
match
operation,
console.log(inp.match(rg));
I get the output as 123213
, denoting that the expression is greedy. But when i use *
, instead of +
, the expression does not return a match. Please help me understand why an empty match is returned. Shouldn't it too be returning the same result.
Since the doc says about ?
,
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".
I would expect an empty match only on applying /\d*?/
.