Javascript regex challenge issue

Tell us what’s happening:
The problem is that even when you write the right solution the test aren’t getting passed. The following code solves the challenge, but 2/5 of the tests fail.
Apologies for my grammar and spelling.

Your code so far

let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin|Eleanor).*Roosevelt/g; 
let result = myRegex.test(myString);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Check For Mixed Grouping of Characters

Link to the challenge:

the problem is the use of the g flag that advance the index at which the test method start checking, making tests fail unexpectedly

to find out more about the g flag with the test method you can look at the documentation on the test method

1 Like

the g flag with the test method moves where the check starts so it causes issues if you always want to check the string from the first character

Can you run this /(Eleanor|Franklin).*Roosevelt/g.test('Franklin D. Roosevelt') code in your browser console. It will return true. why the problem is not causing.


yes, this work, but the tests use the same regex multiple times.

let re = /(Eleanor|Franklin).*Roosevelt/g;
re.test('Franklin D. Roosevelt'); // true 
re.test('Franklin D. Roosevelt'); // and this one?

I deleted that post… check my last post instead


but It working with odd number of times i check regex?

because when the test method returns false then the check start again at the beginning of the string.

try without g flag, everything will return true

If the regex has the global flag set, test() will advance the lastIndex of the regex. A subsequent use of test() will start the search at the substring of str specified by lastIndex ( exec() will also advance the lastIndex property). It is worth noting that the lastIndex will not reset when testing a different string.