Regular Expressions: Reuse Patterns Using Capture Groups ***

I am not understanding what the problem is.I need to pass the test “42 42 42 42”

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

1 Like

same here,

Your regex should not match "42 42 42 42" .

If I test “42 42 42 42” on with /(\d*)(\s)\1\2\1/
it does not match…

but test fails…

1 Like

try to remember the special characters for start and end (hint )

1 Like

You want to specify the beginning and end of your regex for it to work. So instead you should use let reRegex = /^(\d+)\s\1\s\1$/.
This tells regex to look for exactly what you have specified. Instead of what you specified plus whatever it finds in between

EDIT: It won’t match extra in between, only at the beginning and / or end of the string. My mistake sorry :slight_smile:

1 Like

Thanks for the hint, it clearly passes the test but I am not clear on why we need to specify the beginning and end of the regex. What do mean by whatever it finds in between? In between what?

I’m sorry I didn’t do a very good job at explaining it :sweat_smile:

What I meant is, if you don’t use beginning and ending tags, the regex will match anything that contains “42 42 42”, even if there’s more before and / or after that.
This is why /(\d+)\s\1\s\1/ will match “42 42 42 bacon” for exemple. It will return true as long as it finds the specified expression somewhere in the string.

Note that when I said “in between”, it was an error on my side. It will match extra stuff at the end or beginning but not in the middle.

So what you do is tell it that it has to start with one or more numbers, then a space, then those same numbers, another space, and the end with those numbers one last time. This way it doesn’t leave room for anything that you haven’t strictly specified.

Is this clearer? I hope it is :grin: If not I invite you to play around with It helped me understand lots about regex just by playing around with it


Thank you for the clarification and the link!

1 Like

Your solution matches the string that consists only of three repeating numbers separated by space.

From the lesson’s objective:

Use capture groups in reRegex to match numbers that are repeated only three times in a string, each separated by a space.

As I understand the objective is to match numbers separated by space that repeat only three times anywhere in a string . I would assume that means that the group of the matching numbers could be preceded by anything except the same number and space, and followed by anything except space and the same number.