Restrict Possible Usernames 1 condition not fulfilled

Hello there!
I’ve come up with this answer:

let userCheck = /^\D+\w.$/; // Change this line

But it doesn’t check one condition:

Your regex should match the string Jo

Could someone please explain why it doesn’t work since I used the ^\D+ which I believe should accept more than one character that isn’t a number at the beginning of the username?

Thank you very much!

What does the \w. do in your regex?

The \w selects all letters and numbers, plus the underscore.
The ‘dot’, or wildcard is used to replace any one character, so it can be used as any character.
My idea was to combine both so I could finish the username with either a letter or a number without knowing which.
I’m not sure if it was a good idea or if it became a bug, but it’s working for most of the cases. I’d need someone more experienced to tell me which one is correct.

Correct. So \w. will always require two characters in your string. But \D+ is greedy and eats up as many non-digits as it can. So for the string “Jo”, \D+ eats up both of those characters, but you still have \w. wanting two characters as well. Do you see why “Jo” doesn’t pass?

1 Like

I’m forcing the username to be at least 4 characters long because \D+ = 2 characters and \w. = 2 characters?

\D+ is at least 1 char, so you’re forcing the string to be at least 3 chars.
Also, “non digit” may include any non digit symbols like “%” or “$”, so it isn’t an appropriate choice.

I see. Thank you for taking the time to help me!