Regular expression: checking username

Restrict Possible Usernames challenge says to make a regular expression for checking usernames with following rules:

  1. Usernames can only use alpha-numeric characters.
  2. The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
  3. Username letters can be lowercase and uppercase.
  4. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

I came up with a solution and it gives correct results but I want to know if there’s a shorter one:

let username = "JackOfAllTrades";
let userCheck = /(^[a-z][a-z]+$)|(^[a-z][a-z]+\d$)|(^[a-z][a-z]*\d\d+$)/i; 
let result = userCheck.test(username);

Now that you have solved the challenge, one thing you can do is click on the ‘Get Help’ button for the challenge and then ‘Get a Hint’. There are two solutions there that you can look at that have slightly more concise reg expressions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.