Capture Groups: confusion regarding the provided answer

Tell us what’s happening:
Tried the answer on a Regex editor, and the only way I was able to match 42, 3 times was : (\d+)\s\1\s\1. This excludes the answer provided.

My reading of the provides answer is:

let reRegex =  /^(\d+)\s\1\s\1$/;

Match the start^ with one digit of more (\d+) followed by a white space \s followed by another one digit or more \1 followed by another white space \s followed by another one digit or more in the end/finishing 1$

Could someone clarify, if I’m getting crazy?

Thnks

Your code so far
/(\d+)\s\1\s\1/


let repeatNum = "42 42 42";
    let reRegex = /^(\d+)\s\1\2\1$/; // Change this line
let result = reRegex.test(repeatNum);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

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

1 Like

Your rationale seems to be correct and by trying it on the challenge with the code below, it worked properly

let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);

Which is using this variable you provided

The code you provided below as a different reRegex variable, it reads /^(\d+)\s\1\2\1$/ instead of /^(\d+)\s\1\s\1$/

Edit: In sum, your first variable works (/^(\d+)\s\1\s\1$/), the “code so far” doesn’t, both the one that has no markup (/(\d+)\s\1\s\1/ due to the lack of ^ and $) and the full example (due to a typo, you have a \2 instead of a \s)