Skip to content
Advertisement

How to set the regex for this in javascript?

Usernames are used everywhere on the internet. They are what give users a unique identity on their favorite sites.

You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.

Usernames can only use alpha-numeric characters.

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.

Username letters can be lowercase and uppercase.

Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

I tried this but it is mentioned as wrong.

 /^[a-z]+d*$/ig

Advertisement

Answer

     let username = "JackOfAllTrades";
     let userCheck = /^[a-z]([a-z]+d*|d{2,})$/i;
     let result = userCheck.test(username);
     console.log(result)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement