Regex-Reuse Patterns Using Capture Groups- Question

Tell us what’s happening:
Describe your issue in detail here.
I don’t get why the test string "42 42 42 42" shouldn’t match.

The Instruction is:
Use capture groups in reRegex to match a string that consists of only the same number repeated exactly three times separated by single spaces.

Based on the instructions my regex expression is /(\d+)\s\1\s\1/ .

But from what I understand the string "42 42 42 42" should match since the string did contain 3 string of the same number separated by a single space. There is just an extra \s42.

And should a string of "cab 24 42 42 42 is in the one" be true as well? Since there is a number that repeats 3 times.

Am I wrong? or is the instruction not clear what I need to do?

Your code so far

let repeatNum = "cab  24 42 42 42 is in the one";
let reRegex = /(\d+)\s\1\s\1/; // Change this line
let result = reRegex.test(repeatNum);

Your browser information:

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

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

So you want the string to start with a 42, end with a 42, and have a third one in the middle. Separated by a space.

Can you find a way to tell your regex to start and end with something?

Is searching for it in between strings not allowed? Should I only search for the start?
I do know that carrot ^ looks at the start and dollar $ looks at the end.

I do know that the answer is this expression /^(\d+)\s\1\s\1$/ due to the don’t match this string "42 42 42 42".
But that would only match with exact string and not in-betweens a large string.
The instruction is not clear if I should only look for that exact string .

That’s exactly right - the starting should only contain the regex match, and nothing else.

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