Reuse Patterns Using Capture Groups_2

Tell us what’s happening:
I’m not passing the 42 42 42 42 test. My code is allowing this to go through, but it shouldn’t. I looked at some solutions and some people put /^(\d+)\s\1\s\1$/ , but I don’t understand why they put a caret and a dollar sign on the expression. The caret sign means negation. Why would they negate the capture values? The dollar sign means a repetition at the end. Why is that there? Can someone explain why those symbols allow this to work? Thanks.

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

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

Hi ACF, go here and read joker314’s explanation. He nails it.

Also, remember this lesson where the caret has another meaning, to say the matched regex pattern starts at the beginning of the string. The caret and dollar sign are saying exactly how the pattern must start and must end.

-Fred

1 Like

Also, tinker with the regex testers around. I use regextester.com, because I like the syntax helps when you mouse over the parts of the regex. It really explains nicely why things are placed where they are.

And that link goes to a saved copy of the very regex you’re asking about. Mouse over the caret there to see what I mean.

1 Like

Thanks. Thanks. Thanks.