亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有小寫、大寫、字母數字、特殊字符和不超過 2 個相同字符且最小長度為 8 個字符的正則表達式

帶有小寫、大寫、字母數字、特殊字符和不超過 2 個相同字符且最小長度為 8 個字符的正則表達式

收到一只叮咚 2021-11-18 09:47:08
我正在嘗試創建一個正則表達式,它允許 4 種主要字符類型(小寫、大寫、字母數字和特殊字符),最小長度為 8,并且連續不超過 2 個相同字符。我試過尋找潛在的解決方案并將不同的正則表達式拼湊在一起,但沒有這樣的運氣!我在Owasp.org上找到了這個^(?:(?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))(?!.*(.)\1{2,})[A-Za-z0-9!~<>,;:_=?*+#."&§%°()\|\[\]\-\$\^\@\/]{8,32}$但是當我需要全部 4 個字符時,它至少使用了 4 個不同字符中的 3 個。我嘗試將其修改為需要全部 4 個字符,但我一無所獲。如果有人可以幫助我,我將不勝感激!
查看完整描述

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 次或更多次

  • $ 字符串結束

正則表達式演示


查看完整回答
反對 回復 2021-11-18
?
慕仙森

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


查看完整回答
反對 回復 2021-11-18
?
慕尼黑5688855

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))+$");


查看完整回答
反對 回復 2021-11-18
  • 3 回答
  • 0 關注
  • 316 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號