Strange behavior in Restrict Possible Usernames

Tell us what’s happening:
So I’m working on Regular Expressions: Restrict Possible Usernames and I know what I have (using /w, instead of matching letters only) doesn’t satisfy the challenge, I’m just trying some things trying to get a hang of RegExs by playing around with it (RegExs seem great and I’m excited about this chapter, lots of fun), but I can’t figure out what is going on here. on let result = userCheck.test(username); The right side of the operation returns false, but the left side returns true. What am I missing?

Your code so far


//let username = "JackOfAllTrades";
let username = "RegexGuru"; // Just trying thing out
let userCheck = /^\w{2,}\d*$/gi; // my line
// let userCheck = /^\w+\w+\d*$/gi; something else I tried that doesn't seem to be good
let result = userCheck.test(username);
console.log(result);  // returns true but
console.log(userCheck.test(username)); // returns flase

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames/

Never use g flag for the test method f you don’t know what it does

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.

(You find this paragraph under examples if you want to read more about it)

Thanks. I had no idea where it was coming from to begin researching. I guess I still didn’t get the global flag, but that’s why I’m experimenting.