Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zenius/87d75fc7d61826f71fa8bcde0e9813b5 to your computer and use it in GitHub Desktop.
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);
@dominick253
Copy link

Thanks! Really bad difficulty spike on this question. Goes from entering /\w/g to literally asking you to do a very complex sequence. Hopefully it get's redone and has a more gradual difficulty increase over the course of the questions.

@komiljohn-drafts
Copy link

komiljohn-drafts commented Feb 28, 2023

You are using curly brackets, but the task requires using only characters from the covered topics, isn't it?

@saurabhranjan007
Copy link

Works 👍

@Cema2019
Copy link

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