【问题标题】:I have to make a regex and cannot figure out the syntax我必须制作一个正则表达式并且无法弄清楚语法
【发布时间】:2016-04-09 13:20:54
【问题描述】:

正则表达式的规则是:至少 8 个字符,至少使用以下之一:(&%$^#@=),至少有 2 个既不在开头也不在结尾的数字 NOR它们在密码内是否可以相邻,至少有 2 个大写和 2 个小写字符。这就是我到目前为止所拥有的。我不知道如何使正则表达式不在乎字符的顺序。我也不知道如何使整个事情需要 8 个字符。

/^[&%$^#@=]{1,}.[0-9(?!0-9)0-9(?!\s)]{2,}[A-Z]{2,}[a-z]{2,}$/;

【问题讨论】:

  • Lookaheads 应该可以帮助您解决问题。我认为您会发现谷歌提供数百万个密码验证正则表达式,并且应该能够根据您的要求采用它们。

标签: javascript regex


【解决方案1】:

这个经过测试的 JavaScript 函数包括一个符合您的验证要求的带注释的正则表达式:

function valid_password(text) {
    /*#!(?#!js re_password Rev:20160409_0700)
        # Password validation with multiple requirements.
        ^                         # Anchor to start of string.
        (?=[^&%$^#@=]*[&%$^#@=])  # Use at least one (&%$^#@=).
        (?=(?:\D*\d){2})          # Have at least 2 numbers.
        (?!\d)                    # Numbers not at the beginning.
        (?!.*\d$)                 # Numbers not at the end.
        (?=\D*(?:\d\D+)+$)        # Numbers not next to each other.
        (?=(?:[^A-Z]*[A-Z]){2})   # At least 2 uppercase.
        (?=(?:[^a-z]*[a-z]){2})   # At least 2 lowercase.
        .{8,}                     # Be at least 8 characters.
        $                         # Anchor to end of string.
    !#*/
    var re_password = /^(?=[^&%$^#@=]*[&%$^#@=])(?=(?:\D*\d){2})(?!\d)(?!.*\d$)(?=\D*(?:\d\D+)+$)(?=(?:[^A-Z]*[A-Z]){2})(?=(?:[^a-z]*[a-z]){2}).{8,}$/;
    if (text.match(re_password)) { return true; }
    return false;
}

请注意,此正则表达式演示了如何在位于字符串开头的位置应用多个 AND 逻辑条件。另请注意,上述正则表达式中数字要求的多个条件可以合并为一个断言 - 它们在此处以详细(多断言样式)呈现,以演示所使用的技术。

希望这会有所帮助!

【讨论】:

  • OP 提到了at least 2 numbers..您的正则表达式与具有一位和两位数字的字符串匹配..regex101.com/r/xA1dM0/1
  • @rock321987 - 不,我的正则表达式匹配正确。您在 regex101 上的示例错误地使用了“m”修饰符和多行测试字符串。
【解决方案2】:

这个正则表达式似乎有效

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

Regex Demo

正则表达式分解

(?=^.{8,}$) #Positive look ahead to check whether there are at least 8 characters
(?!^\d) #Negative look ahead to check that string does not begin with a digit
(?!.*\d$) #Negative look ahead to check that string does not end with a digit
(?!.+\d\d) #Negative look ahead to check that string does not have two consecutive digits
(?=.*[&%$^#@=]) #Positive look ahead to check that string have at least any of the characters present in character class
(?=(.*[A-Z]){2}) #Positive look ahead to check that string contains at least two Upper Case characters
(?=(.*[a-z]){2}) #Positive look ahead to check that string contains at least two Lower Case characters
(?=(.*[0-9]){2}) #Positive look ahead to check that string contains at least two digits

【讨论】:

  • @GriffinObeid 我已经更新了正则表达式..我看错了..它至少是8 字符,我假设它是准确的8..更正它..还添加了一个检查存在至少两位数..
  • 您可以用(?:\D+\d){2,}\D+$ 结束您的模式,而不是(?!^\d)(?!.*\d$)(?!.+\d\d)(?=(.*[0-9]){2})(?=.{8}) 而不是 (?=.{8,}) 就足够了。一般不需要捕获时不要创建捕获组,而是使用非捕获组。
  • 您的正则表达式不起作用,例如它错误匹配:"12345678AA1bb2=a"(您需要在开头和结尾添加 ^ 和 $ 锚点)。
  • @ridgerunner nice catch..它匹配子字符串
  • @CasimiretHippolyte 感谢您的建议。下次会注意的
猜你喜欢
  • 2013-02-14
  • 2021-05-18
  • 1970-01-01
  • 2015-10-09
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多