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);

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/107.0.0.0 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 regexr.com 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.

Can you provide some examples? The challenge/tests can always be improved.

If you have some better solutions, feel free to create a topic in the Contributors sub forum.

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
7
7
7

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

Thanks for reporting. The first solution is easy enough to fix by replacing the \ with a single space character. The second one should probably be removed unless someone can think of a modification for it. Do you have any other solution suggestions that would work?

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.

I agree. However, it is always nice if we can have more than one solution to show other approaches (if possible). How about /^(\d+) \1 \1(?!.)/ for a regex? I know it is not as simple as ending with $, but it is different.

I may be trying to force something that is not necessary here.