更新时间:2022-11-15 11:26:12
您需要删除 [A-Za-z \ n]
并替换 {7 ,63}
, {8,64}
。
使用
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+.]{8,64}$
请参阅演示
也许,您还想将。
添加回前瞻,以便它是还需要:
Perhaps, you also want to add the .
back to the lookahead, so that it was also required:
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+.])[A-Za-z\d!@#$%^&*()_+.]{8,64}$
^
为确保特殊符号不会立即发生,请添加a (?!。* [!@#$%^& *()_ +。] {2})
负向前瞻:
To make sure the special symbols do not occur in immediate succession, add a (?!.*[!@#$%^&*()_+.]{2})
negative lookahead:
^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+.])(?!.*[!@#$%^&*()_+.]{2})[A-Za-z\d!@#$%^&*()_+.]{8,64}$
^^^^^^^^^^^^^^^^^^^^^^^^
请参阅此演示
请注意,很多人会对使用如此长的正则表达式的可维护性问题大肆宣传。您可以将条件拆分为单独的代码段,也可以使用带注释的多行正则表达式:
Note that a lot of people here would scream about mainainability issue of using such a long regex. You can either split the conditions into separate pieces of code, or use a multiline regex with comments:
var rx = RegExp("^" + // Start of string
"(?=.*[a-zA-Z])" + // Require a letter
"(?=.*\\d)" + // Require a digit
"(?=.*[!@#$%^&*()_+])" + // Require a special symbol
"(?!.*[!@#$%^&*()_+.]{2})" + // Disallow consecutive special symbols
"[A-Za-z\\d!@#$%^&*()_+.]{8,64}" + // 8 to 64 symbols from the set
"$");
var re = RegExp("^" + // Start of string
"(?=.*[a-zA-Z])" + // Require a letter
"(?=.*\\d)" + // Require a digit
"(?=.*[!@#$%^&*()_+])" + // Require a special symbol
"(?!.*[!@#$%^&*()_+.]{2})" + // Disallow consecutive special symbols
"[A-Za-z\\d!@#$%^&*()_+.]{8,64}" + // 8 to 64 symbols from the set
"$", "gm");
var str = '.abc@1234\n*abc@1234\nabc@1234.\na@b.c1234\n*abc@1234\nabc@1234.\na@b.c1234\na@b.#c123\na@__c1234';
while ((m = re.exec(str)) !== null) {
document.body.innerHTML += m[0] + "<br/>";
}