3 回答
TA贡献1851条经验 获得超3个赞
您可以使用基于对比度的否定前瞻,使用否定字符类来匹配 0+ 次而不是列出的任何内容,然后匹配列出的内容。
要在一行中匹配不超过 2 个相同的字符,您还可以使用负前瞻和捕获组和反向引用\1
来确保一行中没有 3 个相同的字符。
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9])(?=[^!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*[!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-])(?![a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*([a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-])\1\1)[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]{8,}$
^
字符串的开始(?=[^a-z]*[a-z])
断言 az(?=[^A-Z]*[A-Z])
断言AZ(?=[^0-9]*[0-9])
断言 0-9(?=
断言您认为特殊的字符[^!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*
[!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]
)
(?!
断言不是连续 3 次来自字符类的相同字符[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*
([a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-])\1\1
)
[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]{8,}
匹配任何列出的 8 次或更多次$
字符串结束
TA贡献1827条经验 获得超7个赞
我认为这可能对您有用(注意:该方法的灵感来自此 SO 问题的解决方案)。
/^(?:([a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^@/-])(?!\1)){8,32}$/i
正则表达式基本上是这样分解的:
// start the pattern at the beginning of the string
/^
// create a "non-capturing group" to run the check in groups of two
// characters
(?:
// start the capture the first character in the pair
(
// Make sure that it is *ONLY* one of the following:
// - a letter
// - a number
// - one of the following special characters:
// !~<>,;:_=?*+#."&§%°()|[\]$^@/-
[a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^@/-]
// end the capture the first character in the pair
)
// start a negative lookahead to be sure that the next character
// does not match whatever was captured by the first capture
// group
(?!\1)
// end the negative lookahead
)
// make sure that there are between 8 and 32 valid characters in the value
{8,32}
// end the pattern at the end of the string and make it case-insensitive
// with the "i" flag
$/i
TA贡献1848条经验 获得超2个赞
您可以尝试以下方法吗?
var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
Explanations
RegEx Description
(?=.*[a-z]) The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z]) The string must contain at least 1 uppercase alphabetical character
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.[!@#\$%\^&]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict
(?=.{8,}) The string must be eight characters or longer
或尝试
(?=.{8,100}$)(([a-z0-9])(?!\2))+$ The regex checks for lookahead and rejects if 2 chars are together
var strongerRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,100}$)(([a-z0-9])(?!\2))+$");
添加回答
举报