Regular Expressions problem

Why does the first regExp works for “A1”, but the second one doesn’t?
The only difference is that there is an extra \d in the first one, but I can’t figure out how that is important. In the second regExp after the ''or" operator, it says find the string that begins with a letter from the alphabet and then it gets followed by one or more numbers that are supposed to be in the end. Shouldn’t it match positive for A1? Actually my question is why it doesn’t?

Your code so far

let username = "JackOfAllTrades";
let userCheck = /^[a-z][a-z]+\d*$|^[a-z]\d\d+$/i; // First regExp
let userCheck = /^[a-z][a-z]+\d*$|^[a-z]\d+$/i; // Second regExp
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/85.0.4183.102 Safari/537.36.

Challenge: Restrict Possible Usernames

Link to the challenge:

\d on its own looks for exactly 1 numerical digit.

\d+ looks for 1 or more digit.

So \d\d+ would look for 2 or more digits.

In your question A1 has only 1 digit.

1 Like

That still doesn’t make sense to me, if \d\d+ means 2 or more digits and \d+ looks for 1 or more, then how does the first one matches A1 but the second one doesn’t?

The first one:

let userCheck = /^[a-z][a-z]+\d*$|^[a-z]\d\d+$/i; // First regExp

does not match A1 (which is what the challenge is asking for)

1 Like

Oh…my apologies…it said should not match, I’m so sorry. Thanks a lot for your patience good sir!

There’s a really great tool called Regex101 that you could use to check stuff like this. It breaks down what each part of the syntax does. I put your second regex into it here:

1 Like

/i at the end makes the regex case insensitive so the careful work you put into [a-z] doesn’t take effect.