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.
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.
@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”?
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 424242”
(/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.
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!”