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
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/reuse-patterns-using-capture-groups/
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
.