Try the hint solution but doesn't work either way

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

Hello I have a problem with a regex exercise. I try and I copy past the Hint solution and it doesn’t work either way, i think there is a problem with this test.

Your code so far


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

** message result :**
// running tests
Your regex should not match the string 42 42 42 42.
// tests completed

If a try with .match the message result is the same

let repeatNum = "42 42 42";
let reRegex = /(\d+)(\s)\1\2\1/;
let result = repeatNum.match(reRegex)
console.log(result) // same

console.log result :

[ '42 42 42',
  '42',
  ' ',
  index: 0,
  input: '42 42 42',
  groups: undefined ]

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

Hyy @audebertadrien.pro,

Good question buddy, even I got stuck when I was there. But later I found the right way.

As the challenge says, it should only match “42 42 42” and not “42 42 42 42”. Both here contain three “42”, so both will get matched.

Now try below code :

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

Why I’ve used ^ and $?
I’ve used them, so that the first and last number should strictly be “42” and there should be only number in between which is “\1”, i.e “42” and not two "42"s, there is only string with that value which is “42 42 42”.

Hope you got your answer. You can reply to this message if you still have questions.
Have a great day ahead! :rose:

2 Likes

Thank you very much @imGarvish.
I have been misled by the return of the console and I didn’t think otherwise than freecodecamp made a mistake. Crazy as I am :slight_smile:

let result = repeatNum.match(reRegex)**[0]**
console.log(result) // => 42 42 42 (even  if i put 4 or more numbers)