Problem with regex pattern

Tell us what’s happening:
Since I’ve also studied regex in python, I wrote a pattern that satisfies given challenge, but I know that there’s an easier and efficient pattern than mine. Can anyone guide me to get an efficient pattern?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^([a-zA-Z]{2}|[a-zA-Z]{2,}\d*|[a-zA-Z]{1}\d{2,})$/; // Change this line
let result = userCheck.test(username);

Challenge: Restrict Possible Usernames

Link to the challenge:

  • [a-zA-Z]{2} isn’t needed because [a-zA-Z]{2,}\d* includes that case (exactly two letters followed by no numbers.
  • The {1} in [a-zA-Z]{1} isn’t needed because “exactly once” is the default in a pattern.
  • Because you know that you always must start with a letter, you can pull [a-zA-Z] out in front of the group to make your regex easier to read.

The above changes got me to this.

/^[a-zA-Z]([a-zA-Z]+\d*|\d{2,})$/

1 Like