Reuse Patterns Using Capture Groups struggle

Tell us what’s happening:
Can sb tell me where I am wrong :frowning: My regex should not match “42 42 42 42”. How can I fix it?

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/69.0.3497.100 Safari/537.36.

Link to the challenge:

pattern.test(str) is checking if pattern exists in provided string. In your case it is true, even for ‘42 42 42 42’ string, because it matches ‘42 42 42’.

You have to figure out how to match string with only 3 repeats, not 2, not 4, but only 3. Try using anchors for matching beggining and end of the string.

1 Like

hi, i make an example for you. This match just with “42 42 42”

// Logic: ( (digit {two times} and space {one times}) {two times}, and number {two times} )
var exp= /^(?:(?:\d{2}\s){2}\d{2}){1}$/;
var x = new RegExp(exp);

var text="42 42 42";
console.log( x.test( text ) );

I fixed it…

This is quite bad example. You force patern to match only two-digits long numbers. Besides it still matches ‘42 42 42 42’ string. Challenge is not about extracting 3 groups of numbers from string, but check if the string contains exactly 3 groups.

var text="42 42 42 42";

var reg=/(?:(?:\d{2}\s){2}\d{2})/;


actually it matches every 3 groups of 2-gitis numbers, because you do not use group matching in your pattern:

var text="41 42 89 2";

var reg=/(?:(?:\d{2}\s){2}\d{2})/;


@tutkun I think you make it too complex and forgot about challenge title - Reuse Patterns Using Capture Groups. And providing ready solution instead of hints is not a good way to learn. Your pattern is still matching random set of 3 * 2-digits numbers:

var text="41 42 89";

var reg=/^(?:(?:\d{2}\s){2}\d{2}){1}$/;


The simplest way to avoid it is just reusing captured groups as the challenge title’s suggesting.
@zuc0n has almost good pattern - he has to make really small adjustment to it.

1 Like

hi @wawraf,
Thank you for informations. actualy my english is bad. I misunderstand the question… :sweat_smile:

So, is enough the following example?

var regex=/^(\d+)\s\1\s\1$/;

Click on the challenge link and check it yourself or here.

If your answer is working then be kind to edit your post and put it in [spoiler]answer[/spoiler] tags to hide it by default.

I’m still learning as well :slight_smile:

Wow! That is awesome. Actualy, I begin learn now of the site intention. :smiley: Thanks

Thank you for your explaination. I figured it out by adjusting into: /^(\d+)\s\1\s\1$/. But I’m quite not really understand. Can you explain to me? Thanks in advance.

You can transform /^(\d+)\s\1\s\1$/ to:

  1. ^(\d+) that matches a certain group (in this case any number) and anchor ^ says it must be beginning of the string, and brackets ( ) captures it for later use.
  2. \s that matches one space.
  3. \1 matches group captured in point one.
  4. \s that matches one space.
  5. \1$ matches group captured in point one and anchor $ says it must be the end of the string.

Therefore we have three identical groups separated by space and two conditions: group 1 must be at the beggining of the string and group 3 must be at the end of the string, hence string 42 42 42 will match it, becase first 42 is at the beginning of the string and third 42 is at the end of the string.

But string 42 42 42 42 will not match, because first 42 is at the beginning and third 42 is not at the end (there is also 4th group) - second condition is not fulfilled.
On the other hand if third group would match last 42 (4th) in this string we meet second condition, but first 42 will not be at the beginning (will be 2nd) and in this case first condition is not fullfilled.
So in string:

'42 42 42 42'
 #1 #2 #3 #4

You can’t match #1#2#3, because #3 is not at the end, or #2#3#4, because #2 is not at the beginning.

I hope I explained it simply enough.


Thank you so so much. I hope I can get more explaination from you. It’s very clear and easy to understand.

Thanks so much for your help @wawraf, but I think the challenge is phrased pretty badly. It doesn’t mention that the pattern should be at the beginning and the end, so how are we supposed to know that a case like “52 42 42 42” should return false? And why would a “ciao 42 42 42” need to return false?
The condition asks only "to match numbers that are repeated only three times in a string, each separated by a space." Doesn’t mention that the string has to be only made of that specific pattern.
That was at least what I wasn’t understanding.
Your explanation though is clear and concise, thanks so much.