How is this wrong?

Tell us what’s happening:

When clicking the run tests button it passes all except one:

Your regex should not match:

"42 42 42 42"

Your code so far


let repeatNum = "42 42 42";
let reRegex = /(\d{2,})\s\1\s\1/i; // Change this line
let result = reRegex.test(repeatNum);

This looks like an error on the validation check. Even if I run this on an external regex tester it doesn’t match 42 42 42 42.

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups

Check your second line.
Maybe?

reRegex = /^(\d+)\s\1\s\1$/;

My point is, I don’t think it’s wrong. Your code works and that is what the solution is. But my point is that my code should work too. Let’s break it down:

(\d{2,}) - First capture group must be 2 digits or more.
followed by a space char
followed by the first capture group
followed by a space char
followed by the first capture group

This matches 42 42 42 perfectly and not 42 42 42 42. To match the latter you’d need to add another \s\1 on the end of my code.

I think this is an error in the lesson. If you someone can correct me, don’t just give me the solution but please explain why mine is incorrect, as I don’t believe it is.

See third party regex tool which confirms my code is ok https://regex101.com/r/aHzVQR/1

No error in the lesson. Your regex will match “42 42 42” (the first three
42s) in the string “42 42 42 42” and it should not.

HI Randell,
that’s my point though; you have confirmed there is nothing wrong with my regex but the lesson says there is. Try it yourself, paste my regex in the lesson and click “run all tests”, it will tell you it’s wrong.

Your regex finds a match when looking at a part of string so it returns true. What that means - your regex is indeed not matching every character in “42 42 42 42”, BUT it doesn’t need that, it finds “42 42 42” in, which matches the specified regex. Take a look in the tool you linked, it say exactly the same thing - not all characters in the given string are matched, but part of the string generates a match, what is enough to return true in the test. It’s not interested in the rest of the string at that point.

so is my regex right or wrong?

Please run the test yourself as you are not understanding my point; that is, I believe my regex is correct. The issue I am raising is that the lesson is wrong, well not the lesson but the validation steps performed when you click “run all test” Just try it yourself. Use my my regex and then click the button “run all tests” in the challenge. It will fail stating “you regex should not match 42 42 42 42” thus implying that DOES match it.

It’s not correct and yes, we are not understanding each other.

Well, it does match it, doesn’t it? Specifically - given regex gets match in the “42 42 42 42” string.

Not sure how to explain this. Take a look at https://regex101.com/r/aHzVQR/2 I’ve only added /g flag (so it checks for multiple matches) and expanded the string. In such case your regex would get 3 matches on the given string.

Somewhat similar situation would be if you had string that contains letters, digits and other characters. And in regex you’d check /\d/ Such regex would get match regardless of the other characters in the string.

I still don’t get it. Well to be more specific I don’t get why my regex apparently matches 4 42’s. I see that it would match if adding the g flag…but mine doesn’t use the g flag so I don’t see your point. Also, when you run the my regex in that regex tool it clearly only highlights three 42’s. The last 42 isn’t highlighted so how can you possibly say that my regex matches 4 42’s!!!

Is the tool I used wrong? Because if you are saying my regex equals 4 42’s then the tool must be wrong. You are confusing me lol.

To quote you, maybe this is the crux of me understanding what you are saying:

given regex gets match in the “42 42 42 42” string.

Are you are saying that my regex falls within (for lack of a better interpretation) the 42 42 42 42 string therefore it matches?

test will return true if it finds a match, not if all the string is matched

\a\.test("strawberry") returns true because there is an a in the word

you need to impose that the match should be of all the string

that’s why people were pointing you toward anchors

Ah I get it now, the penny has dropped!

It is how I have been interpreting it; when I see the text “xxx should match/or not match” I am thinking this in English and not regex. When talking about the spoken languages version of the word “match” it is an exact match so in my case my regex doesn’t match 42 42 42 42. But in regex coding “match” only means that the regex pattern matches somewhere in the string provided. You can see why I took it this though, I mean the error message is in English right so I interpreted the word match like that too.

1 Like