Alternative Solution for "Reuse Patterns Using Capture Groups"

Tell us what’s happening:
Hey, i looked into the solution and understand the usage of ^ (to start) and $ (to end) the testing-line.

But is there an alternative solution?
I thought maybe like saying: there is not another capture group allowed: i tried it with [^/1] . But then it is always false.

Or maybe with something like {3) for the whole regex :smiley: But it doesnt work either.

If there is no other solution or it its very complicated and not for beginners, i am also fine with it.

I am just curious.

Your code so far

let repeatNum = "42 42 42";
let reRegex = /(\d{2,}) \1 \1 [^\1]/; // Change this line
let result = reRegex.test(repeatNum);


Here is my actual solution for the current challenge (so that nobody need to explain me how to get the challenge done like in the solution)

let repeatNum = "42 42 42";
let reRegex = /^(\d{2,}) \1 \1$/; // Change this line
let result = reRegex.test(repeatNum);


PS: i didnt use \s like in the solution because its also fine with a white-space.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

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 challenge given was pretty specific, so there is not much room to improvise. Of course, the syntax could be slightly different, but the solution given is really the only way to do it.

Hey there,

I have a couple questions as well regarding the exercise. I would be grateful if somebody could help me with them. ^^

The first one is also the one that kept me from actually solving the exercise for some time.
So, when I add
^ and $
to the code line, as it is expected, I get a null response from console.log. Can someone explain why this happens? It actually bugged me quite a bit as I refrained from trying out the code seeing as the console of FCC was throwing me something other than:

Your test output will go here

Second question. Though I managed to understand most of the logic behind the code needed to solve the exercise, and more or less found the solution without seeing the solution, I fail to understand exactly how using ^ and $ does what we want them to do.
If we were to “translate” the code, I read it as:

=> search at the beginning of the document
=> for a repeating number group of at least one character
=>show me a copy of that group
\1 followed by $
=>show me a copy of that group that is situated at the end of the string.

After putting into words my frustration, I can see that my initial idea of \1 $ doesn’t make much sense to me either anymore.
Still I don’t exactly understand why the whole code is “translated” as “search in the given string for a number group that is repeated exactly 3 times, no more, no less”.
Personally, I perceive it as “search in the given string for a repeating number group, show it me and copy-paste it 2 more times”.

Sorry for the long post, and thanks in advance for your help and time!

  1. with the correct regex, regex.test() should return a boolean to display in console.log(). But you can always do regex-only tests on sites like

  2. your translation seems fine and I’m not too sure where you are confused. They want a string of only 3 repeating groups of numbers, so it follows the regex must start with ^ and end with $ to make sure stuff like 'aba 22 22 22' or '23 23 23foo' doesn’t test true.
    You find your capture group (\d+), which js then stores to match 2 more times, with a space separating the repeating numbers, making it a total of 3 times.

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