Bug in Regular Expressions: Restrict Possible Usernames?

So I was working through the problem below and I thought I had the answer but it was saying I was wrong. When I logged a match to test what was going wrong, it suddenly worked. In the photos, the top one fails, but the bottom one passes, there’s no difference except that single console.log line.


I went back and forth a couple times trying out different thing, and it seems that I can only pass the assignment if I add a line matching any variable to userCheck (doesn’t just have to be username). I don’t have to log the match either just need to declare it. Anyone know why this would happen ? Lmk thanks.

Hi @vidithbalasa!

Welcome to the forum!

Screenshots are hard to read on the forum. Can you post your code along with a link to the challenge.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

@vidithbalasa It looks like you changed up your tests a little bit. Although, it is hard to read. @jwilkins.oboe Had some good advice. Refer to that comment and it will be easier for us to help. Happy coding and welcome to the FreeCodeCamp forums! :smile:

Your initial code fails because of the global flag /g and how it works with test() and the lastindex property (i.e. if you remove the global flag your code will pass).

I’m honestly not 100% sure why the call to match is resetting the lastIndex property, but that’s what it looks like.

let username = "JackOfAllTrades";
let userCheck = /^[a-z]{1}\d{2,}$|^[a-z]{2}[a-z]*\d*$/gi; // Change this line
let result = userCheck.test(username);
console.log(userCheck.lastIndex) // 15

let test = "A11"
console.log(test.match(userCheck))
console.log(userCheck.lastIndex) // 0

Side note: We should maybe be resetting lastIndex before running the tests, I know we did this with different regex challenges.

Issue