javascript - js regexp - group priority -


lets say, there multiple regular expressions

  1. .+
  2. (?:bbb )?ccc

which combined in single expression groups - /^(first) (second)$/

both groups should not "know" each other (meaning - can't change expressions).

/^(.+) ((?:bbb )?ccc)$/.exec('aaa bbb ccc'); 

the current result:

 ["aaa bbb ccc", "aaa bbb", "ccc"] 

the expected result:

 ["aaa bbb ccc", "aaa", "bbb ccc"] 

how prioritize groups bbb ends in second one?

make first .+ (which inside capturing group) non-greedy adding reluctant quantifier ? next +

^(.+?) ((?:bbb )?ccc)$ 

demo

> /^(.+?) ((?:bbb )?ccc)$/.exec('aaa bbb ccc'); [ 'aaa bbb ccc',   'aaa',   'bbb ccc',   index: 0,   input: 'aaa bbb ccc' ] 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -