Reuse-patterns-using-capture-groups

Tell us what’s happening:

Hello guys!

I think I’ve found a problem on this challenge. All other tests pass except this one:

<Your regex should not match “42 42 42 42”.>

Logically my regX should work just fine and to be sure I’ve already tested it on https://regexr.com/ and all is as expected by matching only the first 3 x “42” num and their 2 in between spaces and falling short of the 3rd space and 4th “42” num.

Hope someone will see this and maybe shed some light on this problem.

Thanks!

Your code so far



let repeatNum = "42 42 42";
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 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3350.0 Safari/537.36.

Link to the challenge:

1 Like

Hi @Madness2aMaze

The challenge requires you to match when a string is repeated ONLY three times in a string. Your regex still passes on 42 42 42 42 because it will match the first 3 42’s as you’ve explained, however, that is not what the challenge is looking for because 42 repeated 4 times in that string.

6 Likes

Hi @joesmith100,

Thank you for your answer!

Let’s see if I got that right, so in order to pass that test in particular, my regX should include a section that is excluding all the strings that have above 3 sub-strings that match the requirements? Then and ONLY then that test will be satisfied. If so, I’m going back at it. :slight_smile:

Yes, that is correct!

So 42 42 42 matches, but: 42 42 42 42 won’t match, 42 42 won’t match, 42 42 42 42 42 42 won’t match and so on…

1 Like

Alrighty then, I get the picture :slight_smile:. Thanks again for your timely explanation !

Hi,

In case someone is interested, the solution was just a matter of defining a beginning and an end to my original regX. So there is no problem with the challenge and I take that back.

This can be closed, if so one desires.

4 Likes

I’m still struggling to implement your explanation. Could you extend a little help ? (Am i correct to assume that i have to use $ for the end of the string ?)

4 Likes

Hello @Kay!

Yes, your assumption about the end of the expression is right and don’t forget to define the beginning of the expression too, using the other special character reserved for that purpose.

You’ll figure it out. Hope you can understand the logic of the expression too.

Cheerio!

2 Likes

Quick question on this challenge: I have a working solution that appears to be the one alluded to in this topic (defining our regex with the beginning and end of expression appended in both sides, respectively) but this would not work if the test strings were similar too “abc 12 12 12 abc”, for instance.

Is there an alternate solution that would work for that type of string? Thank you.

P.S. - I am unsure if we are meant to post the solutions or not so I will refrain from doing it

1 Like

I can see what you did but don’t understand it. Why are there 2 /1 capture group statements?
You’re saying the first group can be a digit with a space or just a space?

Note. I googled the solution with explanation