我尝试使用以下条件验证密码输入字段:
我使用以下内容:
function checkStrength( password ) {
var min_length = /^[\s\S]{8,}$/,
number = /[0-9]/,
special = /[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`´{|}~]/;
if( min_length.test( password ) && (number.test( password ) || special.test( password )) ){
return true;
} else {
return false;
}
}
我想我的问题是是否有办法合并正则表达式,或者我的表达式是否有问题。
答案 0 :(得分:1)
这是你可以使用的正则表达式。
请注意,在分隔符表单/.../
中,分隔符/
必须为
如果在正则表达式中的任何位置,则在正则表达式中转义\/
(你的角色类中有一个)。
# /^(?=.{8})(?=.*(?:\d|[\\!"#$%&'()*+,\-.\/:;<=>?@\[\]^_`´{|}~]))/
^ # Beginning of string
(?= .{8} ) # Lookahead for at least 8 characters
(?= # Lookahead
.* # 0 to many, any character, to get to one of the following
(?:
\d # A digit
| # or,
[\\!"#$%&'()*+,\-./:;<=>?@\[\]^_`´{|}~] # A special character
)
)
# Because we were just looking ahead, we are still
# at the beginning of the string here.
# Optionally match the entire line.
# .+ $
此外,数字和特殊字符可以合并为一个类。
# /^(?=.{8})(?=.*[\d\\!"#$%&'()*+,\-.\/:;<=>?@\[\]^_`´{|}~])/
# Beginning of string
^
# Lookahead for at least 8 characters
(?= .{8} )
# Lookahead for a digit OR a special character
(?= .* [\d\\!"#$%&'()*+,\-./:;<=>?@\[\]^_`´{|}~] )
# Optionally match the entire line.
# .+ $