Tell us what’s happening:
I have the code below and I understand why it does not match Z97, but I’ve not been able to figure out how to fix it. Any ideas would be appreciated.
Your code so far
let username = "JackOfAllTrades";
let userCheck = /^[A-Za-z]+[A-Za-z]+\d*$/; // Change this line
let result = userCheck.test(username);
Your browser information:
User Agent is:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.
Challenge: Restrict Possible Usernames
Link to the challenge:
The failing test says:
Your regex should match Z97
So that is one letter plus two numbers. But your regex starts with:
It asks for two letters. To get your code to work, you have to plan for both situations …
I find this tool helpful, to get regex to work: https://regexr.com/
If you have more questions, please feel free to ask.
Yes, I’m aware that I need to plan for both situations but I haven’t figured out how to do that (other than just looking at the solution). Thanks for the link. I checked it out but I have no idea what it’s for or how to use it. I didn’t really see any explanation or instructions.
The “|” or operator is a really good hint.
On regexr you can enter all your test cases at once. While you fiddle with the code it highlights the matches. Just make sure you use the “m” flag. (flags are in the upper right corner)
Besides the freecodecamp exercises, these where my favorite interactive exercises:
OK, thanks for the info and the reminder to use the or/alternation | operator.
I already had tried a few things with that before posting, but as it seems that I was moving in the right direction I will try that again.
Thanks for the link to the cheat sheet.