Are there any alternative answer to this?

Continuing the discussion from Reuse Patterns Using Capture Groupz:
In this solution, we are basicly saying there should be nothing before and after what we are looking for, are there any other solutions to this puzzle?

let repeatNum = "42 42 42";
let reRegex = /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);

other than writing something like /^(\d+)(\s\1)\2^/ which is mostly the same thing, I don’t think there is much variation here, considering the objective is to match a specific pattern

you could use [0-9] instead of \d, you could use the counter {1,} instead of +, but they mean the same thing

The question is:

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

But when I try this:

let repeatNum = “42 42 42 a”;
let reRegex = /^(\d+)\s\1\s\1$/; // Change this line
let result = reRegex.test(repeatNum);

Result returns false, thus the answer is not entirely correct.

your regex will match only a string that has a number repeated three times, and only that in the string

which is what the challenge is asking

you need to use more complex regex to match exactly three consecutive numbers in a string with other content

Regex is for matching strings. That string is the character “4” then the character “2” then the character " " then the character “4” then the character “2” then the character " " then the character “4” then the character “2” then the character " " then the character “a”.

There isn’t any such thing as a number (like 42) in a string. There are characters that represent numbers, but they are just characters. When you use \d that means match one of the characters “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8” or “9”. Regex has no idea what a mathematical number is

I disagree, but thanks for the comments anyway. :blush: Have a great day!

what do you disagree about?
I think I was explaining about that regex

if something was not clear please say so so I can explain in a different way

I disagree about what the challenge is asking.

I don’t know, the example seems to point to that, plus the tests…

also, it would be too complex for a simple challenge like that to have something like a 24 24 24 a be matched, but not something like a 24 24 24 24 a

the example shows /(\w+)\s\1/

probably with lookaheads and lookbehinds…

also, a 24 24 24 24 a should not pass, but what about a 24 24 24 23 a? there is not enough infos on all those other edge cases to think that it asks more than that

Yes, you are right about the intention of the practice.

I just could not let it go I guess.

it could be your personal challenge, it is an awesome way to practice regex