Tell us what’s happening:
I would like someone to walk me through the answer, please. This is how I thought my current answer would work:
-
^(\d+)\s
- At the beginning of the string, find any digit followed by a space. -
\1
- Repeat that pattern, which is(\d+)\s
. So currently I would have['42 42 '].
-
\d+$
- At the end of the string, find any digit. So I’d assume I would have['42 42 42']
. I’d have three digits because there were three \d+'s. - I didn’t call on the pattern again.
I’m wrong. The solution is /^(\d+)\s\1\s\1$/;
. I understand it up until the next ...\s\1$/;
I understand that /^(\d+)\s\1
would give me ['42 42 ']
because the pattern is called two times, once explicitly and another using \1, which is what I put. So I’m thinking we’d be up the string at the third digit. But why is there another space followed by the same pattern? Wouldn’t the regex try to find ['(42 space) (42 space) space (42 space)']
?
Your code so far
let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\d+$/; // 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/84.0.4147.135 Safari/537.36
.
Challenge: Reuse Patterns Using Capture Groups
Link to the challenge: