Skip to content
Advertisement

Why does this regex capture a maximum of 2 capture groups and not all those within the input string?

JavaScript

Even if you try with several input_text in all cases it is limited (at most) to capture the first 2 matches, but not all the occurrences that actually exist

JavaScript

This should be the correct output, that is, when it succeeds in identifying all occurrences and not just the first 2 matches.

JavaScript

It’s quite curious because if I invert the order of the capture groups within the string, the pattern will detect them, but always limited to the first 2. It is as if the re.sub() method had passed the parameter to replace n number of times (in this case like 2 times), but in that case I am not indicating that parameter, and even so re.sub() just works a limited number of times.


EDIT (with findall):

JavaScript

Advertisement

Answer

you can capture the specific part in the matched string using capture groups and then validate the string is present or not.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement