Regular Expressions - Restrict Possible Usernames - Inconsistency?

Tell us what’s happening:
Here’s the (admittedly incorrect) solution I’m working on, but testing against the same string 5 different times results in alternating boolean responses. Please see the picture below.


My intent was not to test the same string 5 different times, but recreate some of the test cases. I just observed the behavior as I was working.
Am I missing something obvious?

Your code so far

let username = "JackOfAllTrades";
let userCheck = /[\w\D]+\d*$/ig; // Change this line
let result = userCheck.test(username);
console.log(userCheck.test("BadUs3rnam3"))
console.log(userCheck.test("BadUs3rnam3"))
console.log(userCheck.test("BadUs3rnam3"))
console.log(userCheck.test("BadUs3rnam3"))
console.log(userCheck.test("BadUs3rnam3"))

Your browser information:

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

Challenge: Regular Expressions - Restrict Possible Usernames

Link to the challenge:

It’s because you’re using the g flag, I think. I seem to remember that that has some kind of state associated with it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.