javascript 正则表达式匹配具有两个或多个特殊字符的强密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4670639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:30:02  来源:igfitidea点击:

Regex match a strong password with two or more special characters

javascriptregexpasswords

提问by rwyland

I need to regex match a password field using javascript with the following requirements:

我需要使用具有以下要求的 javascript 正则表达式匹配密码字段:

  • At least 15 characters
  • two or more lower case letters
  • two or more upper case letters
  • two or more digits
  • two or more of the following special characters: !@#$%^&*-
  • 至少 15 个字符
  • 两个或更多小写字母
  • 两个或多个大写字母
  • 两位或更多位数字
  • 两个或多个以下特殊字符:!@#$%^&*-

I have a regex that takes care of MOST cases:

我有一个处理大多数情况的正则表达式:

/^.*(?=.{15,})(?=.{2,}\d)(?=.{2,}[a-z])(?=.{2,}[A-Z])(?=.{2,}[\!\@\#$\%\^\&\*\-]).*$/

The problem here is with the symbols, it works with:

这里的问题在于符号,它适用于:

P@ssw0rdP@ssw0rd
Pssw0rdPssw0rd@@
Pssw0rd@@Pssw0rd
P@ssw0rdP@ssw0rd
Pssw0rdPssw0rd@@
Pssw0rd@@Pssw0rd

But not:

但不是:

@@Pssw0rdPssw0rd
@@Pssw0rdPssw0rd

I have a random password generator set up to exhaustively test this, so any ideas are greatly appreciated. Thanks!

我设置了一个随机密码生成器来详尽地测试这个,所以非常感谢任何想法。谢谢!

回答by kennytm

/^(?=(?:.*[a-z]){2})(?=(?:.*[A-Z]){2})(?=(?:.*\d){2})(?=(?:.*[!@#$%^&*-]){2}).{15,}$/


Your lookaheads are wrong. The pattern

你的前瞻是错误的。图案

(?=.{2,}[class])

means to match 2 or more characters (no matter what characters), then followed by 1 character of the desired class. This is entirely different from "2 or more character of the desired class" you specified.

表示匹配 2 个或更多字符(无论是什么字符),然后是所需类的 1 个字符。这与您指定的“所需类别的 2 个或多个字符”完全不同。

To correctly test if a character of desired class is in the text, use

要正确测试文本中是否存在所需类别的字符,请使用

(?=.*[class])

and since you want to check it twice, repeat the pattern

并且由于您想检查两次,请重复该模式

(?=.*[class].*[class])
# equivalent to (?=(?:.*[class]){2})

回答by Martin Jespersen

I'm not sure a single regexp is the way to go for this test.

我不确定单个正则表达式是否适合进行此测试。

Personally i'd implement it something like this: (treat as pseudo code, i haven't tested it)

我个人会像这样实现它:(视为伪代码,我还没有测试过)

function testPassword(pw) {
    var len = pw.length;
    if(len < 15) return false;
    if(pw.replace(/[a-z]/,'').length > len - 2) return false;
    if(pw.replace(/[A-Z]/,'').length > len - 2) return false;
    if(pw.replace(/[0-9]/,'').length > len - 2) return false;
    if(pw.replace(/[!@#$%^&*-]/,'').length > len - 2) return false;
    return true;
}

回答by Martin Jespersen

There are some good explanations already, so I'm just piling on ...

已经有一些很好的解释,所以我只是在堆积......

/^
(?= .{15} )
(?= (?:.*[[:lower:]]){2} )
(?= (?:.*[[:upper:]]){2} )
(?= (?:.*[[:digit:]]){2} )
(?= (?:.*[!@#$%^&*-]){2} )
/x

/^
(?= .{15} )
(?= (?:.*[[:lower:]]){2} )
(?= (?:.*[[:upper:]]){2} )
(?= (?:.*[[:digit:]]){2} )
(?= (?:.*[!@#$%^&*-]){2} )
/x