Last active
April 15, 2024 19:59
-
-
Save zenius/87d75fc7d61826f71fa8bcde0e9813b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Works 👍
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).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are using curly brackets, but the task requires using only characters from the covered topics, isn't it?