Reputation: 5340
On a string 'abbc'
This regexp /(.)\1*/g
gives 3 matches ('a'
,'bb'
and 'c'
).
Very similar regexp /((.)\1*)/g
gives 4 matches (('a'
,'b'
, 'b'
and 'c'
).
I always assumed, that the outside group does not and can not change the result. How that is even possible?
See https://regex101.com/r/5fX9My/1
Upvotes: 0
Views: 31
Reputation: 19651
Your second pattern doesn't match the same as the first one. When you enclose your pattern in a capturing group, that group becomes group #1. So, in your first pattern, \1
is a backreference for (.)
while in the second pattern, \1
is a backreference for the outer group.
To create a pattern that's identical to the first one but with an extra outer group, you should use:
((.)\2*)
Upvotes: 1