-
-
Save zenius/87d75fc7d61826f71fa8bcde0e9813b5 to your computer and use it in GitHub Desktop.
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. | |
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. | |
Solution: | |
let username = "JackOfAllTrades"; | |
let userCheck = /^[a-z]([a-z]+\d*|\d{2,})$/i; | |
let result = userCheck.test(username); |
Explanation from Gemini:
^[a-z]: Matches a single lowercase letter at the beginning (enforces no leading numbers).
([a-z]+\d*|\d{2,}): This is a capturing group that matches either:
[a-z]+: One or more lowercase letters (for usernames with letters only).
\d*: Zero or more digits (allows numbers at the end).
|: OR operator separates the two options.
\d{2,}: Two or more digits (enforces two-character usernames to be all numbers).
$: Matches the end of the string.
i: Case-insensitive flag (allows both lowercase and uppercase letters).
Thank you Cema for that explanation... one question though... it says: 4) Usernames have to be at least two characters long.
A two-character username can only use alphabet letters as characters.
You said \d{2,} enforces all numbers. Isn't that incorrect? Although it worked for me...
Works 👍