如何启用在正则表达式的密码验证中添加特殊字符

时间:2018-10-13 08:00:03

标签: javascript jquery regex

我正在使用this Regular Exp来验证密码输入,但这不接受任何特殊字符

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/

解释如下:

/^
  (?=.*\d)          // should contain at least one digit
  (?=.*[a-z])       // should contain at least one lower case
  (?=.*[A-Z])       // should contain at least one upper case
  [a-zA-Z0-9]{8,}   // should contain at least 8 from the mentioned 

个字符     $ /

您能告诉我如何在表达式中添加以下内容吗?

/[!@#$%\^&*(){}[\]<>?/|\-]/

1 个答案:

答案 0 :(得分:1)

如果要确保存在特殊字符,只需添加另一个正向超前组,然后将特殊字符添加到匹配组中即可:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%\^&*(){}[\]<>?/|\-])[0-9a-zA-Z!@#$%\^&*(){}[\]<>?/|\-]{8,}$/