This post may…be in the wrong place but this code also works, I’m showing it so maybe admin can pass it in future? I don’t see why it SHOULDN’T pass
Your code so far
let username = "JackOfAllTrades";
let userCheck =/^[a-z]\w+[0-9]*$|^[a-z]\w+\[0-9][0-9]+$/i // Change this line
//(\w+)
///^\w\w+[0-9]*$|^\w\w+\[0-9][0-9]+$/i (this line also passes in fiddle)
let result = userCheck.test(username);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
The \w+ in your regex is causing issues with the matching. Remember that \w matches [a-zA-Z0-9_] so you may end up matching more than you expected. Let’s break it down using examples that shouldn’t pass:
D9 - invalid because two-character usernames can’t end with a digit. Notv4lid0 - invalid because digits can only be at the end.
And we’ll only need to look at the first part of your regex:
^[a-z]\w+[0-9]*$
In the case of username D9, it is considered valid by the first part of your regex, which checks that:
The first character is [a-z] (D)
There are one or more characters that match [a-zA-Z0-9_] (9)
And there are zero or more characters that match [0-9] at the end (true, there’s nothing)
In the case of the username Notv4lid0, the first part of the regex is also the problem:
The first character is [a-z] (N)
There are one or more characters that match [a-zA-Z0-9_] (otv4lid0, greedy matching)
And there are zero or more characters that match [0-9] at the end (true, there’s nothing left).
If you replace the \w+ in the first part of your regex, you should be able to remove the part after the | completely and pass the exercise.
You can also always check your regex on a site like regexr or regex101. Use the strings from the tests. They also give a rundown of what is being matched.