Restrict Possible Usernames lesson

Hi guys I am stuck. I have tried different variations I believe this is my best one yet.
I have restricted numbers in the beginning, got letters only in middle, ending with a number, yet BadUs3rnam3 still get matched and some more failures, would love some tips :slight_smile:

edit: so far this is my best variation /^\D[a-zA-Z]\d*$/;
Your code so far


let username = "JackOfAllTrades";
let userCheck = /\D[a-z]\d*/i; // Change this line
let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Restrict Possible Usernames

Link to the challenge:

A regular expression will match on any part of a string. The only way to specify the beginning or end of the string are with the characters ^ and $.

For example, the regular expression /Hello/ will match “Hello World”, but it will also match “Don’t say ‘Hello’!”

so I have added a caret before \D
thats what I’ve done /^\D[a-z]\d*/i;
how come it doesn’t match c57bT3 now ( which is good ) but i don’t understand.
\D matches anything but numbers character c = isn’t a number. so why a caret changes it, shouldn’t it search for anything but numbers anyway since it’s at the beginning of the regex?

edit : also if I put a $ sign at the end of my regex example
/^\D[a-z]\d*$ to look for numbers at the end, now it doesn’t match JACK, which makes sense JACK doesn’t have numbers and should pass, so I am a bit confused what to do.

without $ jack passes, but I get numbers in the middle which again I find confusing since my regex doesn’t have numbers in the middle :frowning:

edit: i am feelnig like im getting closer I thought , I can only use ^, $ at the end or beginning of a regex, but it doesn’t matter.

It sounds like you’re getting there on your own. The solution to this isn’t a super-simple regex, so don’t get discouraged.

/^[a-zA-Z][a-zA-Z]+\d*?$/

the closest I have gotten, I am missing to match Z97, any suggestions? ty :slight_smile:

Your current regular expression requires 2 or more letters at the beginning.

isn’t it, this? 2 or more characters at the beginning?

Right. That requires two letters at the beginning, meaning that “Z97” will fail because it only has one.

so I am supposed to do Usernames have to be at least two characters long
and if its 2 chars must be letters, not sure how to do it tho.

/^[a-zA-Z][a-zA-Z]+ should I put a question mark at end of 2nd one?

If I recall the requirements correctly, I believe you will want to use an “or” operator (|) because part of the string can be either one pattern or another.

/^[a-zA-Z][a-zA-Z]+\d?\d*?$|^[a-zA-Z][\d*$]\d$/

I just completed it but honestly I don’t even understand how lol.

how would you read brackets inside your head?
for now for example [a-zA-Z] I read it as the string must have a char between a-z both capital and smaller letters, but what is the difference between that and a-zA-Z? that is a bit confusing to me.

I would read the square brackets as “one of” or “any of” in my head.
/a-zA-Z/ would match the literal characters “a-zA-Z”. The - symbol only indicates a range because it is in the brackets.

1 Like

Thank you for the clarifying answer so without bracket it literally matches a-z. This clarified a lot of my confusion! :slight_smile:

One more question … ;p
I take a look at the answer to learn from it

/^[a-z][a-z]+\d*$|^[a-z]\d\d+$/i;

reason word JACK passes is because of the * after \d correct? that means even if no numbers appear at the end it’s okay ( 0 or more ).

Right. It is two or more letters followed by zero or more numbers.

1 Like

Another question
Shouldn’t technically this Z97 not pass?

since it requires numbers at the end and technically 9 is in middle?

^[a-z]\d\d+$

This is “starts with a single letter, followed by one digit, followed by one or more digits, then nothing”

[spoiler]/^[a-z][a-z]+\d*$|^[a-z]\d\d+$/i;[/spoiler]

talking about this, shouldn’t technically Z97 not pass because it asks no numbers in the middle?