Regular Expressions - Reuse Patterns Using Capture Groups

Tell us what’s happening:

Describe your issue in detail here.
So simply from left to right, can someone explain to me how this works/passes? In particular, I do not understand why the “^” or “$” are necessary. Also, just to understand capture groups, is the “\1” (used twice) indicating we want the first capture group to repeat two more times (a total of 3 times) in this example?

Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Regular Expressions - Reuse Patterns Using Capture Groups

the anchors ^ and $ match the beginning and end of a string, so that means that your regex will match a string exactly from beginning to end. Otherwise it could match a pattern in the middle of a string with around many different things.

You can use tools like regex101.com (for JavaScript you need to set FLAVOR to ECMAScript)
and it will give a breakdown like
image

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