Regular Expressions - Restrict Possible Usernames

Hello
I’m trying to understand why this solution passes the j%4 when it shouldn’t (well the goal is that it will not pass but my solution somehow passes it and I do not understand why?).

The result I’m getting is:
// running tests
Your regex should not match the string J%4
// tests completed

As far as I understand, only a string that begins with at least two [a-z] letters followed by zero or more numbers at the end, can pass, or just one [a-z] letters at the beginning followed by two or more digits (including underscore, yes I’m aware of that, but not %).
Can someone explain it to me?

Your code so far

let username = "JackOfAllTrades";
let userCheck = /((^\D{2,}\d*$|^\D{1}\d{2,}$))/gi; // Change this line
let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15

Challenge Information:

Regular Expressions - Restrict Possible Usernames

What does the character class \D represent?

Matching all but numbers
All letters lower and uppercase and underscore
\d (lowercase d) matches numbers only

This is correct.

True, but if it matches anything but numbers, it would match more than just letters and underscore, wouldn’t it?

1 Like

Hmm… yep :slight_smile:
Missed that completely !

Thanks

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.