Regular Expressions: Restrict Possible Usernames - What is missing?

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.
Thanks!

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:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames

The failing test says:

Your regex should match Z97

So that is one letter plus two numbers. But your regex starts with:

^[A-Za-z]+[A-Za-z]

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.

Hi @Sharad823, i always seem to struggle with anything other than the most basic regexp too. Ive did this exercise some time ago and when looking to solve it gain just now I had to referred to this cheat sheet (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet) to help me out. Hopefully not giving to much away by pointing you in the direction of the " | " for alternation and you need to look at capturing groups with ( ) and quantifiers using {}

1 Like

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)

Screen Shot 2020-03-06 at 16.18.33

Besides the freecodecamp exercises, these where my favorite interactive exercises:
http://play.inginf.units.it/#/

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.