Regular Expressions - Restrict Possible Usernames

My regex was the same as solution but I used [\d] ( inside brackets) at the end and it didn’t work.
What’s the difference of using \d and [\d] at the end ?

Could you share your full version? It doesn’t seem that replacing [\d] with \d should matter here.

Actually, my version was not exactly the same as the solution.
But it did work after some changes and I guess it was not because of using \d or [\d].
The important thing is that you answered my question: it doesn’t matter using \d inside brackets or not in this case (freeCodeCamp solution).
I’m wondering when it should make the difference. It’s still not clear for me.
Thank you for your reply

The square brackets are used to match a character from a group of possible characters:

[abcd] // matches "a" or "b" or "c" or "d"
[a-d] // same as above using hyphen syntax
[0-9] // matches any number from 0 to 9

The last one is so common that there is an even shorter version:

\d // same as above

So using [\d] is just unnecessary use of the square brackets since \d will do what you need. But if you wanted to match either a number or white space then you would do:


I got it now.
Very good explanation.
Thank you.

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