0
/.*?/.exec("abc");//output [""]

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

1
  • If you want just one character, you can use /./.exec('abc') without any modifiers Commented Jan 7, 2014 at 10:50

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

6 Comments

but there is no empty char in abc. you mean it also can match a position like ^ $ \b ?
Yes it definitely can. In this case it is just matching ^
can you please show me some articles about .* match a position? it's a little weird.
Better to go through this post: regular-expressions.info/zerolength.html
@looping Think of it in this way. /a(b*)c/ should obviously match "ac", what does the capturing group (b*) capture?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.