Reuse Patterns Using Capture GroupsPassed

Tell us what’s happening:
I am confused by the solution and how it works.
why do I need ‘^’ at the beginning and '' in the end and why does my code not return true when the regex is "/(\d+)(\s)\1\2\1/g"? Also, my knowledge of why I need ‘$’ at the end of the pattern is still iffy. Please elaborate on that too. Thanks!!

Your code so far


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

Your browser information:

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

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

^ and $ means start of string or line and end of string or line respectively. If you don’t use these, your regex will match strings like repeatNum since you’re not specifying your regex where to start and stop looking, since we’re supposed to look for numbers that repeat themselves exactly three times each separated by a space.

Also you can’t use the /g modifier here; you’re already using capture groups.

1 Like