Regular Expressions - Reuse Patterns Using Capture Groups

Hi,
Please explain why my code is wrong:
let repeatNum = “42 42 42”;

let reRegex = /(\d+) \1 \1/g; // Change this line

let result = reRegex.test(repeatNum);

let result2 = repeatNum.match(reRegex);

console.log(result);

console.log(result2);
Thanks
Sara

Your code so far

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

Your browser information:

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

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

your code will match also the string 42 42 42 42, instead it must match exactly three numbers, not more

This is a side comment as a good hint was given by another user (which is the hint in the console as well.)

Forget it if the following is confusing.

The g flag may not hurt, but it may not be needed either.

If it is at the beginning, and must end with it, g isn’t necessary.

For example, should we want the numbers " meow! 42 42 42" I’d use g .

Just be aware that the written regex won’t match 42 43 42. For this one we could use /(\d+) \d+ \1/, for example.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.