Restrict Possible Usernames Explanation of \D

Tell us what’s happening:
I got this challenges right after putting \D/ig and D only matches non-numbers character but on the question it says.“The only numbers in the username have to be at the end. There can be zero or more of them at the end.” meaning i could also find for numbers on my code.
Can someone explain to me how the \D character summarises them all?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /\D/ig; // Change this line
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/72.0.3626.109 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/restrict-possible-usernames

It’s best to describe the regex you want in English first, then write it in regex syntax. Eventually you’ll skip right to “thinking in regexes”, but the extra step is very useful for starting out. The trick is to translate the three rules into a single statement on what the regex should be. I’ll just give you the English version for this particular one:

“A username starts with two or more letters, followed by zero or more digits at the end. A username is not case-sensitive.”

The “starts with” and “at the end” should clue you in that you need the ^ and $ tokens in your regex. You already know from the previous lessons how to say “two or more” and “zero or more” at this point, so all that’s left is to fill in the syntax for “letter” and “digit” and assemble it all into one regex.

Hopefully that’s enough hints to get you the rest of the way. Try writing the regex and give a shout if you’re still stuck.

1 Like

Oh BTW, the explanation of \D you asked for: you can in this case represent the regex as “Two or more non-digit characters followed by zero or more digits”. I’m partial to matching letters specifically, since \D will match space and punctuation (anything that isn’t a digit), which is IMHO a little fast and loose for usernames, but using \D for the non-digit parts will still satisfy the challenge tests.

1 Like

well explained thank you so much

It doesn’t, it’s just able to pass the tests.
I advice you to check how test() works when you use the g flag, it has a special behaviour there.

1 Like