0

In Chrome,

/{.*?}/g.exec('aaa{eee}uuuu')

returns

["{eee}"]

, whereas

/{(.*?)}/g.exec('aaa{eee}uuuu')

returns

["{eee}", "eee"]

The second result is what I expected. Why not the first code return the naked string of "{eee}"?

Nigiri
  • 3,469
  • 6
  • 29
  • 52
  • 3
    Because u captured it in second case using `()` and not in first case – vks Feb 03 '16 at 05:41
  • 3
    Why did you expect that result if you didn't place a [capturing groups](http://www.regular-expressions.info/brackets.html) in your expression? – Bergi Feb 03 '16 at 05:45
  • I didn't know the word and the concept "capturing". Thank you very much. – Nigiri Feb 03 '16 at 05:55

1 Answers1

1

Because in first regex you did not used braces. braces is used to group the string passed but in second regex you have used braces which group "eee" according to your input.

First regex returns array with only one element that matches. but in second expression it return array with 2 element . [0] => whole string matched, [1] => string matched inside braces. If more braces are used then it will return [2] => ...,[3] => ... , etc

refer: JavaScript Regex Global Match Groups

Community
  • 1
  • 1
Vegeta
  • 1,319
  • 7
  • 17
  • Refer: http://stackoverflow.com/questions/19913667/javascript-regex-global-match-groups – Vegeta Feb 03 '16 at 05:46
  • it wont throw error but return array with only one element that matches. but in second expression it return array with 2 element . [0] => whole string matched, [1] => string matched inside braces. If more braces are used then it will return [2] => ...,[3] => ... , etc – Vegeta Feb 03 '16 at 05:52