This part of the regex seems to look for special characters:
(?=.*[ !@
Note that the character class does not contain underscores, try changing this to the following:
(?=.*[ _!@
You will also need to modify or delete this part of the regular expression:
(?=.*\W+)
\W
equivalent to [^a-zA-Z0-9_]
, so if the underscore is your only special character, this part of the regular expression will crash. Instead, change it to the following (or delete it, it will be superfluous, since you already check for special characters before):
(?=.*[^\w_])
Full regex:
/(?=^.{8,}$)(?=.*[ _!@ #$%^&*-])(?=.*\d)(?=.*[^\w_])(?![.\n])(?=.*[az])(?=.*[AZ]).*$/
source share