Restrict Possible Usernames - what is wrong?

Restrict Possible Usernames - what is wrong?


So this is my code now:


How come it still passes a string with a number at the front?



Again, you do not need the A-Z because you are using the i flag at the end of the regular expression.

Hint: To solve your dilemma, think about a special character you learned about which defines the beginning. Also, do you really need both the * and the ?


I got it :smiley:

The ^ was what i needed, and a $ at the end.




This is my first post but I felt like I wanted to share my solution.

I have not praticed Regular Expressions before FreeCodeCamp.

I just skimmed most of the posts in this thread but the solutions look quite long to me.

My solution:

let userCheck = /^\D\D/i;

The solution checks that the two first characters in the string are not numbers and is also case insensitive.

Please feel free to give me feedback on this solution.

Edit: When I look at my solution again I think that it might match non-alphabetical characters in the first two characters (but still passes the test). There might be room for improvement in checking the answer.


It might have passed all the tests given by FCC, but yours is not a solution to the given problem. All the previous posts here had dealt with this issue.

Let me give you a counter example.
a123 should match because it ends with a number and has more than 2 characters. However, your regex fails to match with this one.

The minimum requirement for this regex must have the form /^...$/, otherwise there will be incorrect matches.

If you have any question, consider opening another post because this one is already solved.


My solution and it works



Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, try to just give hints and examples of other code which will help the camper instead of the final solution.

Thank you.


How will yours handle when input is 123abc?


Here is a hint i gave someone else on this same problem:


Thanks for the nice posts. After reading this helped figure out the solution. I broke the reasoning down to this, if this helps anyone else:

must be alphabet letters: [a-z],
alphabet letters must be a minimum of first 2 characters: {2,},
only numbers at the end of the string, any amount allowed: \d*$,
not case sensitive: /i

This solution worked, took a while :slight_smile:

let userCheck = /^[a-z]{2,}\d*$/i;


Use the shortcuts. There is an easier solution. I worked backwards from what worked and what didn’t

let userCheck = /[\w]+[^\d$]/i ;