user56690
user56690

Reputation: 259

javascript regular expression to recognize all special characters

I am writing a regular expression for a stronger password which needs atleast one small case letter, upper case letter, one number, one special character and has length=8 or above.

I have the below one regular expression.

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}/

The problem with this one is it is not considering underscore('_') character as a special character. How can I include this in the list of special characters?

Thanks in advance.

Upvotes: 2

Views: 66

Answers (1)

SamWhan
SamWhan

Reputation: 8332

_ is a word-character and thus not included in the \W (not word) class. Put the \W inside [] and add the _ like

/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).{8,}/

That should do it

Regards

Upvotes: 2

Related Questions