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+[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
Challenge: Restrict Possible Usernames
Link to the challenge:
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:
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 may have seen the solution in the guide, but here’s what I came up with that works in the problem:
or a shorter equivalent
Match a single a-z character at the beginning of the string, as we know there will be one.
Next comes either one or more a-z characters, with greedy matching, or two or more numerals (which comes up in the exercise).
Then, there are zero or more numerals at the end.
Your solution shouldn’t work though…because the question involved has an “option” so you’d need an “or” operator here
Yeah, I see what you mean here…one would NEED the [a-z] with the i, although…one could also use the \D (for non-digits, in place of [a-z])
That’s what this is for:
The “or” is in the middle.
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.
My apologies, I saw it (or) when I looked again, didn’t see before, my bad…
Yeah, I know about these, I just used fiddle because it works with js that’s all
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.