开发者

Regex to test whether there are more than two digits in a string

开发者 https://www.devze.com 2023-04-07 03:31 出处:网络
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.

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]开发者_JAVA技巧.*[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
}


You can try

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

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


You can just use:

\d.*\d

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消