Specify Exact Number of Matches to limit a pattern

Tell us what’s happening:
Hi campers, I already completed the problem through the asked method but I’d really like to understand why this one won’t work. Basically I think I’m trying there to test this pattern to only exist once through the string (of course not ha ha).

Your code so far

let repeatNum = "42 42 42";
let reRegex = /((\d+) \1 \1){1}/; // Change this line
let result = reRegex.test(repeatNum);
let Match = repeatNum.match(reRegex);
console.log(Match);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

This regex will not work for a couple of reasons. The first reason is, since you are wanting to match 3 of the same number separated by spaces, the capture group containing the number is the 2nd and not the first, since you created two capture groups. However, correcting this part will not allow you to not match 42 42 42 42. Why? Because the second issue with the regex is even though you specified {1} hoping to only match 3 of the same number one time, this will match in two different ways:

  • the first three 42s
  • the last three 42s

Thx Randell, it’s clearer but I still can’t see it crystal clear though. Why does the number only pertain to the 2nd capture group ?

The first capture group is between the first set of ( and ). The second capture group (d+) which represents the number.

Crystal clear now. As I added the global parentheses after having created my first capture group and his reps, I mislead myself into thinking the global capture group was the second one.
Thanks a lot Randell !