repeatNum Regex issue

Tell us what’s happening:
Hi,

I am not sure why the 4th item 42 in the string “42 42 42 42” is also matching as true with the below regex /(\d*)\s\1\s\1$/;
Can anyone please comment and explain why the 4th number which is differentiated by space is also matching

Your code so far
let repeatNum = “42 42 42 42”;
let reRegex = /(\d*)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);
console.log(result);



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

you are saying that your string should end with three identical numbers, there is no restriction on how it should start, so that passes

1 Like

Thanks for the answer but I still couldn’t get logic that why it is not stopping after 3 matches since in regex I have put only 3 matches for \d.

My understanding is as below
Regex = /(\d*)\s\1\s\1$/
String = “42 42 42 42”

(/d*)/s = 42 space
/1/s = 42 space
/1$ = 42 End

here you are saying how the string should end

as there is nothing saying how it should start all of these can pass:

43 43 43
re 101 101 101
4 5 5 66 66 66
abracadabra 1 1 1

how do you impose the start of a string?

1 Like

Thanks a lot for explaining this helps me get the point now