Because you’re not re-using the groups like you think you are.
Let’s look at this step by step. We start with “42 42 42 42”. I’ll place all the matches in a string array for clarity.
- this matches groups of digits, no spaces
- we get
[ '42', '42', '42', '42' ]
- matches digits with just one 1 space following it
- and we get
[ '42 ', '42 ', '42 ' ]
- if we stop here, we’d have 3 matches of your group
[ '42 '] // #1
- this is the group that gets repeated
- this repeats your capture group once wherever you place it
- so you’re saying:
repeat [ '42 ' ] once
- now we get
[ '42 42 ']
- we only get one match because the required repeating pattern of
[ '42 42 ' ] only happens once. The rest of the string looks like this
[ '42 42' ]. The last ‘42’ has no space after it.
- this matches any number of digits after repeating your match
- so take my match of
[ '42 42 ' ] and match if there’s a group of digits following
- finally we have
[ '42 42 42' ] // #2
- we stop matching because the remaining string is
[ '42' ] which doesn’t match any requirement
The reason you’re getting confused is because you think it’s repeating the capture group twice. But the numeric reference
\1 only repeats in the location where it is placed.
In the console, you’re getting the result of the match, followed by the capture group that produced this result
["42 42 42", "42 "]
Index 0 is the match, which fits what we see at #2, and index 1 is the capture group which we see in #1
So you’re returning a match, while the test expects you to not match. There’s a clue in this line as to what to match and repeat
Your regex should have two spaces separating the three numbers.
and this one
Your regex should reuse the capture group twice.
Find a way to satisfy these specs while using
Hope that clears it up some more. Let me know if there’s something I didn’t explain clearly.