Reuse Patterns Using Capture Groupz

Tell us what’s happening:
My mind is blowing. How do I limit BOTH number of digits in one number AND number of repeats? (Match Only 3 numbers with at least 2 digits in it => 42 42 42 or 100 100 100 but never 42 42 42 42);

Your code so far


let repeatNum = "42 42 42";
let reRegex = /(\d+)\s\1\s\1/; // Change this line
let result = repeatNum.match(reRegex);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

It is possible to set up a regex to have a specified beginning and end. That way you dont have to worry about finding 3 out of 4 possible numbers, but only exactly 3.

^… is specifing the beginning.
…$ is specifying the end.

If you use both you tell the regex to only look for exactly what is between ^ and $. Try it with your regex and see what happens.

6 Likes

Thank you very much, sir! It worked!

Thanks, I solve my problem : )

@myrmidonut This challenge really threw me for a loop because in the hints section of this challenge it mentions using the global “/g” to indicate that a regex will keep looking for a match I assumed that withholding the /g meant that it would stop looking once it found a exact match for the regex I wrote.

Does hint 3 make sense to you? or would using the /g only apply when using a “.match” instead of a “.test”?

Is there anybody who can tell me whats wrong in my code?

let repeatNum = "42 42 42";
let reRegex = /(\d+)(\s)\1\2\1/; // Change this line
let result = reRegex.test(repeatNum);

Here is my attempt at explaining this. Your code will match “42 42 42 42” because it meets the criteria you included in your RegEx. It will see that the second, third, and fourth part of the string matches your RegEx: “42 42 42 42

(/d+) --> will be the second 42,
(\s) --> will be the space after the second 42
\1 --> will be the third 42
\2 --> will be the space after the third 42
\1 --> will match the fourth (last) 42

Hence, your code fails because it matches the input with 4 numbers. Your code will give the same result if you provided it a string with “100 100 100 100”.

A hint, remember you can use ^ to state what we want at the beginning of a match, and $ to state what we want at the end. With that in hand, then you just focus on what should be in between.

Best of luck. And I hope this was helpful and makes sense.

7 Likes

Thanks @aglope it helped me understand this.

Thanks a lot. I had the same problem (my regex was /(\d+)\s\1\s\1/) and I just thought that it wouldn’t match “42 42 42 42” because it would stop after the first match but I can see my mistake now.

So, by including a ^ at the beginning, and a $ at the end, we are basically telling it the string needs to be literally this with no extra stuff? It’s a little weird, because if I’m saying “I need 42 at the beginning, and 42 at the end” it does follow that requirement still. It’s just that it takes this literally in the middle as well? Basically…“Ok I started with 42, then space, then 42, then space, then 42…but I’m not at the end! So this isn’t right!”

Just trying to parse this in my head.

1 Like