正则表达式测试字符串中是否有超过两位数字
据我所知, \d{2,}
匹配2个或更多连续数字,但我需要知道字符串中是否有2个数字。
我还希望获得一些密码强度计的良好链接。
我现在用的是
function passwordStrengthPercent(pwd,username)
{
var score = 0, special = /(.*[!,@,#,$,%,^,&,*,?,_,~,;,:,`,|,\\,\/,<,>,\{,\},\[,\],=,\+])/
if (pwd.length < 8 ) return 0
if (pwd.toLowerCase() == username.toLowerCase()) return -1
score += pwd.length * 4
score += ( checkRepetition(1,pwd).length - pwd.length )
score += ( checkRepetition(2,pwd).length - pwd.length )
score += ( checkRepetition(3,pwd).length - pwd.length )
score += ( checkRepetition(4,pwd).length - pwd.length )
if (pwd.match(/(.*[e].*[e].*[e])/)) score -= 15//most common letter in passswords?
if (pwd.match(/(.*[a].*[a].*[a])/)) score -= 15//most common letter in passswords?
if (pwd.match(/(.*[o].*[o].*[o])/)) score -= 10//most common letter in passswords?
if (pwd.match(/^\l+$/) || pwd.match(/^\d+$/) ) return score/2//there was w here in regexp
if (pwd.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 10
//todo additional rules 11
if (pwd.match(special)) score += 15
if (pwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 15
if (pwd.match(/(w)/) && pwd.match(/(d)/)) score += 15
if (pwd.match(special) && pwd.match(/(d)/)) score += 10
if (pwd.match(special) && pwd.match(/(w)/)) score += 10
if ( score < 0 ) return 0
if ( score > 100 ) return 100
return score
}
As far as I know, \d{2,}
matches 2 or more consecutive digits, but I need to know whether there are any 2 digits in a string.
I would also appreciate some good links to password strength meters.
What I use now is
function passwordStrengthPercent(pwd,username)
{
var score = 0, special = /(.*[!,@,#,$,%,^,&,*,?,_,~,;,:,`,|,\\,\/,<,>,\{,\},\[,\],=,\+])/
if (pwd.length < 8 ) return 0
if (pwd.toLowerCase() == username.toLowerCase()) return -1
score += pwd.length * 4
score += ( checkRepetition(1,pwd).length - pwd.length )
score += ( checkRepetition(2,pwd).length - pwd.length )
score += ( checkRepetition(3,pwd).length - pwd.length )
score += ( checkRepetition(4,pwd).length - pwd.length )
if (pwd.match(/(.*[e].*[e].*[e])/)) score -= 15//most common letter in passswords?
if (pwd.match(/(.*[a].*[a].*[a])/)) score -= 15//most common letter in passswords?
if (pwd.match(/(.*[o].*[o].*[o])/)) score -= 10//most common letter in passswords?
if (pwd.match(/^\l+$/) || pwd.match(/^\d+$/) ) return score/2//there was w here in regexp
if (pwd.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 10
//todo additional rules 11
if (pwd.match(special)) score += 15
if (pwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 15
if (pwd.match(/(w)/) && pwd.match(/(d)/)) score += 15
if (pwd.match(special) && pwd.match(/(d)/)) score += 10
if (pwd.match(special) && pwd.match(/(w)/)) score += 10
if ( score < 0 ) return 0
if ( score > 100 ) return 100
return score
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试
仅在(至少)包含两个数字时才匹配。
You can try
which will only match if (at least) two digits are included.
您可以使用:
匹配行中任意位置的两个数字,即使它们之间有其他字符。
You can just use:
That matches two numerals anywhere in the line, even with other characters between them.
检查是否有恰好两位数字:
使用 grep 查看测试:
check if there are Exactly two digits:
see the test with grep: