Reuse Patterns Using Capture Groups2

Reuse Patterns Using Capture Groups2
0

#1

Tell us what’s happening:
// running test
Your regex should not match “42 42 42 42”.
// tests completed

Not sure why this is the error message. I’m pretty sure my regex is right but i dont know

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.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

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


#2

If I’m not mistaken your regex is currently the same as /\d+\s\d+\s\d+/, which is not quite right because as long as it finds that matter in a given, anywhere in that given string, it’s a match. So your RegEx expression would match this “nyanpasu 42 42 42”, or this “42 42 42 nyanpasu”, or this “42 42 42 42 42 42 42 42 42” (I think you get the idea)—because it doesn’t care what comes before or after your RegEx, as long as it finds that pattern it’s a match.

I recommend quickly going through the list of RegEx challenges that comes before the one you are doing to get… inspired!


#3

I disagree with @honmanyau . I think the first problem is that you are not incrementing your groups.

So instead of \s\1\s\1 you should use \s\1\s\2 .


#4

I think I have to disagree with the disagreement. d: Reference for the regular expression \n from the MDN Web Docs:

Where n is a positive integer. A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).

There is only one parenthetical in /(\d+)\s\1\s\1/, so you can’t possibly use \2. For example:

const str = "nyanpasu nyanpasu nyanpasu" ;

str.match(/(nyanpasu)\s\1\s\2/); // null
const str = "nyanpasu nyanpasu nyanpasu" ;

str.match(/(nyanpasu)\s\1\s\1/); // ["nyanpasu nyanpasu nyanpasu", "nyanpasu"]

It is worth noting there is only one captured group in the returned array, too.


#5

ah yes. That makes sense actually. My bad.


#6

someone else posted about this and their solution is here: