【问题标题】:Password Regex with XRegExp使用 XRegExp 的密码正则表达式
【发布时间】:2013-07-09 06:42:00
【问题描述】:

我有一个密码字段的客户端验证正则表达式,它应该允许 Unicode 支持的所有语言的字母。

这是现有的密码正则表达式:

(?=^.{8,21}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z])

我正在使用 XRegExp 库,并且我尝试修改正则表达式以包含 \p{L} 但我无法让它传递像“Ss!11111”这样的密码。

下面是我修改过的正则表达式:

(?=^.{8,21}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*\\p{L})

我知道这个问题可能看起来很愚蠢,但我目前没有多少时间来试验这个正则表达式。如果有人帮助我并指出我正确的方向,那将非常有帮助。

【问题讨论】:

    标签: javascript regex validation passwords xregexp


    【解决方案1】:

    你需要把所有的反斜杠加倍:

    (?=^.{8,21}$)((?=.*\\d)|(?=.*\\W))(?![.\\n])(?=.*\\p{L})
    

    另外,我不确定这个正则表达式是否有意义。这真的是你想要的吗:

    (?=^.{8,21}$)  # Assert length 8-21 characters (why only 21?)
    (              # Group 1:
     (?=.*\\d)     # Assert at least one digit in the string
    |              # OR
     (?=.*\\W)     # at least one non-alnum character in the string
    )              # End of group 1
    (?![.\\n])     # Assert that the *first* character isn't a dot or newline (??)
    (?=.*\\p{L})   # Assert at least one letter in the string
    

    【讨论】:

    • 不错的收获..我猜(?![.\\n])应该是(?!.*[.\\n])
    • 太棒了,我不敢相信这是解决办法,哈哈。是的,这就是所需的正则表达式,包括最多 21 个字符 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2012-05-30
    相关资源
    最近更新 更多