why regular expression .*? only match an empty string in javascript?

227 views Asked by At
/.*?/.exec("abc");//output [""]

I think .*? is non-greedy and it should return a

2

There are 2 answers

6
anubhava On BEST ANSWER

Well that is expected since .* means 0 or more and by putting ? you make it non-greedy hence it match an empty string.

If you want to match a then you should use:

 /.+?/.exec("abc");

DIfference is + instead of * which means match 1 or more characters using non-greedy quantifier.

0
guessimtoolate On

By using * instead of e.g. + you allowed for matching empty string as a non-greedy option.