Trouble with Regex Capture Groups Challenge

Trouble with Regex Capture Groups Challenge
0.0 0


I am supposed to make a regular expression that only matches with 3 numbers seperated by spaces. So I did /(\d+)\s\1\s\1/ which correctly matches all of the answers like “10 10 10” and “20 20 20”. My problem is that there is an answer “42 42 42 42” which matches because the first part of it matches my regex(42 42 42). So I tried adding a negative lookahead to make sure there wasn’t anything after that pattern. For that, I made (?!.). So, in the end my regex looked like /(\d+)\s\1\s\1(?!.)/. Why doesn’t that work? Shouldn’t the negative lookahead make it so that it doesn’t match with the “42 42 42 42” answer? Am I writing the negative lookahead wrong?

Thanks for the advice :+1:

Link to the challenge:
My code so far

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


As you can see :arrow_up: the problem isn’t that your negative lookahead is wrong. The problem is that it matches on part of the string. Instead of negative lookaheads, you might wan to look at anchors.


Wow! Didn’t think of that :sweat_smile:
I’ll definately look into anchors! Thanks again Ariel!

For anyone looking into this thread in the future, here’s what I ended up creating that worked:
The anchor at the beginning is important because that makes sure it checks for the pattern from the start of the string!