Restrict Possible Usernames-Spoiler

I solved the following problem with this code:

let username = "JackOfAllTrades";
let userCheck = /\w[a-z]/gi; // Change this line
let result = userCheck.test(username);

/\w[a-z]/gi; Looks like the \w automatically says it cannot start with a number because its the same as [A-Za-z0-9_] but it can include uppercase and lowercase character plus numbers 0-9.

Then the [a-z] says if the username is less than 2 characters it can only contain alphabet letter characters.

Am I understanding and interpreting this correctly?

Here is the breakdown of your regular expression.

\w means Match any word character (alphanumeric & underscore).
[a-z] means match a character in the range of “a” to “z”. It’s case sensitive.

Thanks for clarifying, I juts saw I was also able to pass the test without the g flag and only the i which ignores the letter case while matching /\w[a-z]/i. I just want to know if this \w shorthand character class is like responsible for this point in the test.Does the shorthand character put alphabet letters first and then numbers.

  1. The only numbers in the username have to be at the end. There can be zero or more of them at the end.

Good question. You don’t need g flag because you are testing single words and not multiple words in a string. i is not necessary because /w is case-insensitive.

Does the shorthand character put alphabet letters first and then numbers? No. It can be either way.

I will refer you to my favorite website to test regexs so that you can see on your own :slight_smile:

Gl further.

1 Like

This is actually incorrect, as the challenge specifics

The only numbers in the username have to be at the end. There can be zero or more of them at the end.

Yet nothing in your code actually checks for that, meaning 1jack can pass through without any issue.

Yet nothing in the challenge actually checks for that either. Meaning your code can pass through also.

Thanks for clarifying. That’s why I was wondering why I’m passing the test.

If you need help figuring it out, think of:

How do you specify what should be at the start of the string?

If you do, for instance, \d{2,}, you are checking for two or more numbers.

* means 0 or more

$ is used to check a end of a string.

\w means numbers and letters, so you might want to avoid it. Try the conventional [a-z].

Now think about how much should be at the start and the end of the string.

1 Like

Well I came up with this which makes more sense for me now:


I’ll move on from this challenge I think I’ve got the picture.

1 Like


  1. ^[a-z] -> In the beginning (^) match only letters .(case insensitive because ‘i’ flag), // Rule 2, ok.

  2. [a-z]+ -> then, match any number of letters, but at least ONE. (+) (case insensitive too.) // Rule 3, ok.

  3. [\d]*$ -> finally, check at the end of the string, zero or more (*) digits (\d) // Rule 1, ok.

Hope it helps! :slight_smile:

1 Like

@GitCharly I’ve gone through at least 5 different posts looking a simplistic explanation as to how someone got their answer. Yours is the first to do just that. I wasn’t looking for the answer itself, just a better understanding of why people chose what they chose. So far, yours is the best I’ve seen. As someone who learns best by understanding the “why,” I really do appreciate you taking the time to explain how you came about your solution. Thank you. :trophy: