Reuse Patterns Using Capture Groups - Help!

Tell us what’s happening:
Hi guys
I stuck at this challenge, I spent about 5 hours to figure out but …

Please help me understand this

Thanks in advance

Your code so far


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

Your browser information:

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

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

look to this try also, really I don’t understand

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

I tested it using console.log, The challenge says :
// running tests

Your regex should match “100 100 100”.

Your regex should not match “42 42 42 42”.

// tests completed

???!

let repeatNum = "42 42 42 42 42 42 42 42 42 42 42 42";
let reRegex = /(\d+)\s\1\s\1/g; // Change this line
let result = reRegex.test(repeatNum);
let test = repeatNum.match(reRegex);
console.log(test);
42 42 42,42 42 42,42 42 42,42 42 42

As it should only match for 3 of them, consider using anchors to make sure your regex represents the whole string

1 Like

Also you Don’t need to use the g flag - it gives unexpected results with the test method

1 Like

Thank you so much, I did it with anchors

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

you’re right I removed g and it works. Thank you