javascript - js regexp - group priority -
lets say, there multiple regular expressions
.+
(?: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)$
> /^(.+?) ((?:bbb )?ccc)$/.exec('aaa bbb ccc'); [ 'aaa bbb ccc', 'aaa', 'bbb ccc', index: 0, input: 'aaa bbb ccc' ]
Comments
Post a Comment