Reuse Patterns Using Capture Groups -- help

Tell us what’s happening:

With the code I used here it only captures “2” in the first group instead of “42” and it captures the “42” if I omit the “\1” at the end.

But in the example given in the lesson the pattern I used does capture the entire word. Why is that?

Also, What wrong am I doing with the code?

Your code so far

let repeatNum = "42 42 42";
let reRegex = /(\d+)\s(\d+)\s(\d+)\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/67.0.3396.99 Safari/537.36.

Link to the challenge:

I think you’ve missed the point of the exercise (to not repeat the regular expression which you are repeating 3 times above). I would suggest you reread the challenge from start. But to answer you:

Your current regex reads like the following in english:
I want to find
[one or more digits]followed immediately by one space[followed immediately by one or more digits]followed immediately by one space[followed immediately by one or more digits]folllowed immediately by one or more digits

So of course the only way this works is if the match is:
[2] [42] [4]2

What you want instead in english is:
[[i want to find one ore more digits] followed by one space]] repeat the second group once then repeat the first group once

…or something like that.

1 Like

I tried this:


It passed all tests except: “42 42 42 42”.

I have been practicing on regex101:
While it only matches the first three 42’s, why does it not simply say that the string does not match?

that looks promising. The only thing missing is something to force the check to look for only three times. Right now your regex when applied to “42 42 42 42” can match the first three 42s . You want it to fail if the pattern is repeated too many times…

(so to answer you, the reason it says it does not match, is because it does match. The regex engine doesn’t know that you want to match only three 42s)

so hopefully this gives you a direction to go in (ie. find a way to fail if the number is repeated more than 3 times)

1 Like

Well, I figured that, I just don’t get how. The only way I know is curly brackets {} and I don’t know how to use that in this code.

ok here’s another hint. You need a regex (very very similar to yours) but it also states that your pattern is the first and last thing in the string…


Yes, I got it.


Is it possible to use just one \s in this code? Or what code were you initially suggesting?

great! initially I suggested something that seems to not be supported by the challenge test bucket, so for now, your answer is the best one I could think of. In a real life situation there are many variations, as you can guess, for this regex expr.

1 Like