Regex test fail

Tell us what’s happening:

When I run the test it fails on the following:

  • Your regex should match “42 42 42”.
  • Your regex should match “100 100 100”.
  • Your regex should match “10 10 10”.

but I think the regex is ok, because on console.log(result) it gives me ‘true’ to that strings. Can someone tells me what’s going on? Thank you!

Your code so far

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


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Reuse Patterns Using Capture Groups

Link to the challenge:

can you explain why you are using the global flag?

Hi @danisolo91,

you aren’t ‘matching’ but ‘testing’. test() returns true/false, while match() returns an array with the matched elements.

You’re right! The g flag was there because I was doing some tests before with different strings and forget it there thinking it was ok relying on the console.log :upside_down_face:

Thank you

Hi @michaelsndr,

I thought it was the same, I mean, I used the .test() method just to check if the regex matches something, since I didn’t need to extract the actual matches.

Sorry, sloppy reading on my part. I thought of this:

let reRegex = /^(\d+)\s\1\s\1$/g;
let result = repeatNum.match(reRegex);

which passes, but as a false positive …