Regex capture groups

Tell us what’s happening:
Hey, i have checked the other posts regarding capture groups in this challenge. I am yet to understand why my solution is wrong (or what is the reason for fixating the pattern at the start and at the end of the regex?)
Kindly help me understand, even an analogy would be appreciated.

Your code so far


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

console.log(resultT);
console.log(resultM);

/*
reRegex:
the whole string returns group 0 i.e., \0
(\d+) : is group 1 i.e., \1
(\s) : is group 2 i.e., \2

*/

Your browser information:

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

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

your regex is true for this but also for the following

"A brown fox 42 42 42 jumps"

and for “42 42 15 15 15 42”
etc

the requirement was to have a regex that matches only strings with that exact pattern, nothing more nothing less

1 Like

thank you for the clarification.

that clicked and also the examples.
Asante!

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