正则表达式测试字符串中是否有超过两位数字

发布于 2024-12-06 04:45:50 字数 1500 浏览 2 评论 0原文

据我所知, \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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-12-13 04:45:50

您可以尝试

^.*\d.*\d.*$

仅在(至少)包含两个数字时才匹配。

You can try

^.*\d.*\d.*$

which will only match if (at least) two digits are included.

娜些时光,永不杰束 2024-12-13 04:45:50

您可以使用:

\d.*\d

匹配行中任意位置的两个数字,即使它们之间有其他字符。

You can just use:

\d.*\d

That matches two numerals anywhere in the line, even with other characters between them.

白芷 2024-12-13 04:45:50

检查是否有恰好两位数字:

^[^\d]*\d[^\d]*\d[^\d]*$

使用 grep 查看测试:

kent$  echo "23
2   3
ax3x2x
aaaaa23
a222
2
3x3x4
3 4 5"|grep -P "^[^\d]*\d[^\d]*\d[^\d]*$"                                                                                                
23
2   3
ax3x2x
aaaaa23

check if there are Exactly two digits:

^[^\d]*\d[^\d]*\d[^\d]*$

see the test with grep:

kent$  echo "23
2   3
ax3x2x
aaaaa23
a222
2
3x3x4
3 4 5"|grep -P "^[^\d]*\d[^\d]*\d[^\d]*$"                                                                                                
23
2   3
ax3x2x
aaaaa23
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文