Reuse Patterns Using Capture Groups

Reuse Patterns Using Capture Groups
0

#1

Tell us what’s happening:
I double check my code by regex tester, it should be fine.
https://regex101.com/r/FS9KSe/1

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 (Macintosh; Intel Mac OS X 10_11_6) 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

#3

In case anyone else arrives here, following link has the answer.
Reuse-patterns-using-capture-groups


#4

All good but specify start and stop string match.

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

#5

Can you explain why it won’t work without specifying the start?


#6

It matches both occurrences of “42 42 42”, eg.:
42 42 42 42”
“42 42 42 42


#7

Sorry I am just responding to this. I want to believe that the explanation by @lynxlynxlynx does suffice. In summary, we want it to be able to search “the multiple” from end to end and when it exceeds thrice, it is not a match.


#8

but it did match all the requirements when I test it in that online regex app…


#9

/^(\d+)\s\1\s\1$/
this should be a correct answer but it doesn’t match any requirements online, what’s wrong with that?


#10

You didn’t test it with “42 42 42 42”

And in the screenshot, your test text contains also the parentheses, which is why it doesn’t match.