How did this Regex Pass the test?

How did this Regex Pass the test?
0

#1

Challenge : Regular Expressions: Restrict Possible Usernames
Regular Expressions: Restrict Possible Usernames

My code:

let username = "JackOfAllTrades";
let userCheck = /[a-zA-Z]{2,}/; // Change this line
let result = userCheck.test(username);

apparently it passed all the tests.

I was looking into the forum for some understanding of the challenge but couldn’t figure {2,} since it wasn’t mentioned in the previous challenges

Can anyone give a step by step understanding of the userCheck expression used above?


#2

It means 2 or more instances of the preceding regex.

/[a-zA-Z]{2,}/ means: look for two or more characters from A to Z, both lowercase or uppercase.


#3

As a matter of fact, I think the regular expression shouldn’t pass the tests. I have reviewed the challenge requirements and it explicitly asks you to find a username matching a string STARTING with two letters that can be followed by digits.

The regex you used won’t match that, as those two letters you’re looking for could be anywhere in the string, not just in the beginning. For example, “1JackOfAllTrades” would pass as a valid username and it shouldn’t, right? Have a look at the following screenshot. The regex is matching all 7 lines but should only match the last 4.

Regex

I know the challenge is marked as passed, but can you guess what’s missing in your regex?


#4

Umm… I think \d*$ so as to detect the digits in the end?


#5

Could be :smiley: Try it on regexr.com for example.

Remember you need to check the first 2 characters are not digits.