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條經驗 獲得超8個贊
我認為這可能對您有用(注意:該方法的靈感來自此 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))+$");
添加回答
舉報