带有小写、大写、字母数字、特殊字符和不超过 2 个相同字符且最小长度为 8 个字符的正则表达式

我正在尝试创建一个正则表达式,它允许 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 个字符,但我一无所获。如果有人可以帮助我,我将不胜感激!


收到一只叮咚
浏览 255回答 3
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,}$^&nbsp;字符串的开始(?=[^a-z]*[a-z])&nbsp;断言 az(?=[^A-Z]*[A-Z])&nbsp;断言AZ(?=[^0-9]*[0-9])&nbsp;断言 0-9(?=&nbsp;断言您认为特殊的字符[^!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*[!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-])(?!&nbsp;断言不是连续 3 次来自字符类的相同字符[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]*([a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-])\1\1)[a-zA-Z0-9!~<>,;:_=?*+#."&§%°()|\[\]$^@\/-]{8,}&nbsp;匹配任何列出的 8 次或更多次$&nbsp;字符串结束正则表达式演示

慕仙森

我认为这可能对您有用(注意:该方法的灵感来自此 SO 问题的解决方案)。/^(?:([a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^@/-])(?!\1)){8,32}$/i正则表达式基本上是这样分解的:// start the pattern at the beginning of the string/^&nbsp; &nbsp; // create a "non-capturing group" to run the check in groups of two&nbsp;&nbsp; &nbsp; // characters&nbsp; &nbsp; (?:&nbsp; &nbsp; &nbsp; &nbsp; // start the capture the first character in the pair&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make sure that it is *ONLY* one of the following:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;- a letter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;- a number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp;- one of the following special characters:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp;!~<>,;:_=?*+#."&§%°()|[\]$^@/-&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [a-z0-9!~<>,;:_=?*+#."&§%°()|[\]$^@/-]&nbsp; &nbsp; &nbsp; &nbsp; // end the capture the first character in the pair&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; // start a negative lookahead to be sure that the next character&nbsp; &nbsp; &nbsp; &nbsp; // does not match whatever was captured by the first capture&nbsp; &nbsp; &nbsp; &nbsp; // group&nbsp; &nbsp; &nbsp; &nbsp; (?!\1)&nbsp; &nbsp; // end the negative lookahead&nbsp;&nbsp; &nbsp; )&nbsp; &nbsp; // make sure that there are between 8 and 32 valid characters in the value&nbsp; &nbsp; {8,32}// end the pattern at the end of the string and make it case-insensitive// with the "i" flag$/i

慕尼黑5688855

您可以尝试以下方法吗?var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");ExplanationsRegEx&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description(?=.*[a-z])&nbsp; &nbsp; &nbsp; The string must contain at least 1 lowercase alphabetical character(?=.*[A-Z])&nbsp; &nbsp; &nbsp; The string must contain at least 1 uppercase alphabetical character(?=.*[0-9])&nbsp; &nbsp; &nbsp; The string must contain at least 1 numeric character(?=.[!@#\$%\^&])&nbsp; &nbsp; The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict(?=.{8,})&nbsp; &nbsp; &nbsp; &nbsp; 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 togethervar strongerRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,100}$)(([a-z0-9])(?!\2))+$");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript