Reuse Patterns Using Capture Groups: help

Tell us what’s happening:

using capture groups, looking for numbers repeated 3 times, separated by spaces, but It doesnt work

Your code so far


let repeatNum = "42 42 42";
let reRegex = /(\d{3}\s)\1\2/; // Change this line
let result = repeatNum.match(reRegex)
console.log(result)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups/

\d{3} This matches three sequential digits.
Putting the \s inside the capture group includes it in the group, so if you repeat that group, then you will only match if there is a space at the end.
\2 would refer to a second capture group, but you only have one capture group.

1 Like

Hey, I got this (better but not enough):
any digit repeated 2 or 3 times
a space repeated (I’m feel confused here: why do I need two capture groups?)

let reRegex = /(\d{2,3})(\s)\1\2/; // Change this line
let result = repeatNum.match(reRegex)
console.log(result)

You don’t need two capture groups. The instructions also don’t specify that it should be two or three digits.

The problem was that I didn’t understand how the \1 works