0

I'm fairly knowledgeable with regex, but I still can't rap my finger around this match. I have the following string aaaaaabcaaaaaaa which is matched by the following regex caaa|aab|aaa. What I'm expecting is caaa, aab, aaa, and what I'm getting is aaa, caaa, aaa. Can some regex guru please tell me, why I can't get the expected result with this regex? Thank you.

let string = 'aaaaaabcaaaaaaa',
    regexp = /caaa|aab|aaa/g;
while( match = regexp.exec( string ) )
  console.log( match );
Mujnoi Gyula Tamas
  • 1,759
  • 1
  • 12
  • 13
  • 1
    Regex matches a string from left to right. You get expected non-overlapping matches. – Wiktor Stribiżew Jun 23 '20 at 16:18
  • It's very simple - the beginning of the string matchs the final alteration in the regex. The string is examined in order, the first alteration will not be matched first, if it's all the way at the end of the string. – VLAZ Jun 23 '20 at 16:18
  • @WiktorStribiżew Than how can I change the regex to match in the expected order, without changing the string? – Mujnoi Gyula Tamas Jun 23 '20 at 16:22
  • Just match for each alternative separately. – Wiktor Stribiżew Jun 23 '20 at 16:37
  • But if I have a larger dataset to match, and a larger regex (think dictionary) won't it be inefficient? – Mujnoi Gyula Tamas Jun 23 '20 at 16:51
  • "*But if I have a larger dataset to match, and a larger regex (think dictionary) won't it be inefficient?*" **Yes**, it will be. Which is a reason regexes match the way they do - in sequence, rather than walking back and forth through the string matching different bits from it. – VLAZ Jun 23 '20 at 18:37

0 Answers0