Regular Expressions - Reuse Patterns Using Capture Groups

Tell us what’s happening:
Describe your issue in detail here.

hints: useless/already understood and not helping much in this case.
Solutions: also useless as I don’t understand them.
I almost understand the first one, but why do we need to say that it must start with a number? As it is is works for all cases but " Your regex should not match the string 42 42 42 42" where it matches still because there’s still the 42 42 42 pattern in the string.
putting an $ on the last /1 not helping either.
Not matching in the case of more numbers was never covered in the exercise and so why is it in the test cases?

The second solution seems like something we were never taught and there are no explanations for it. Must have been pretty straightforward to whoever wrote it :slight_smile:
Why can there either be a ‘!’ or not?

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Regular Expressions - Reuse Patterns Using Capture Groups

Link to the challenge:

I will start off by saying that I do not think those example answers are good (and please note that example answers are not the “official” or “best” answers – just answers that are known to pass the tests). While they pass all the tests, they do not truly meet the specification, and thus they would match a number of strings that they should not.

I have set up a page on to help you try out different regular expressions with instant feedback so you can see what works and what does not:
RegExr: Learn, Build, & Test RegEx

You can use regexr or similar sites any time you want to write and test a new regular expression. It is very helpful!

Okay, now let us look at the specification:

Use capture groups in reRegex to match a string that consists of only the same number repeated exactly three times separated by single spaces.

The expression given in the example for finding a repeated word ( /(\w+) \1 \1/ ) is very close to what we want, but obviously we want it to handle numbers only instead of all alphanumeric characters. We just need to replace \w+ with something to catch digits.

Once that is done, you will notice that it matches strings that include larger numbers at the left and right, or really any other characters outside of the three sets of digits; but at least it doesn’t catch tabs, newlines, and other forms of whitespace, like one of the example answers…

What is the proper method to ensure that the string starts and ends exactly with that pattern? You will need to add a certain character on the left, and a different character on the right.

Sure. Example answer #1 (/^(\d+)\s\1\s\1$/) accepts any whitespace, rather than spaces only, as in the specification. Therefore, it will match either of the following:

3	3	3

Example answer #2 (/^(\d+) \1 \1(?! \1)/) matches the following strings:
100 100 1001
100 100 100 101
99 99 99thisisnotevenanumberomg

I think that if you replace each \s with a space in the first answer, you should have the simplest and most accurate expression to match the specification.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.