如何在这个“密码复杂性"正则表达式中允许下划线 [英] How to allow underscore in this 'Password Complexity' regex
问题描述
这是:
/(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/
它仅在密码包含大写和小写字母以及 1 位数字或 1 个特殊字符时才通过,但是我希望下划线 _ 也算作特殊字符,目前还没有,如何修改正则表达式,以便它允许下划线算作特殊字符?
It only passes if the password contains upper case AND lower case letters, and also either 1 digit or 1 special character, however I want underscore _ to count as a special character as well and it currently does not, how can modify this regex so that it will allow underscore to count as a special character?
这里是上下文...
jQuery.validator.addMethod("complexity", function(value, element) {
return this.optional(element) || /(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/.test(value);
}, "password is not complex, see requirements above");
推荐答案
/(?=^.{8,}$)((?=.*\d)|(?=.*[\W_]+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/
aioobe 将 \W 替换为 [\W_]
aioobe was close replace \W with [\W_]
只是让您知道这将空格视为特殊字符.
Just so you know this considers a space a special character.
此外,我没有看到您在哪里检查数字.没关系我找到了.(复杂的正则表达式就像一个 wheres waldo.)
Also I don't see where you are checking for numbers. nevermind I found it. (man complex regexes are like a wheres waldo.)
这是一个不允许空格的简化版本(并且更容易维护)
Here is a simplifed version that does not allow spaces (and it is easyier to maintain)
(?=^.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])(?=^.*[^\s].*$).*$
^ ^ ^ ^ ^
| | | | L--does not contain a whitespace
| | | L--at least one non word character(a-zA-Z0-9_) or _ or 0-9
| | L--at least one upper case letter
| L--at least one lowercase Letter
L--Number of charaters
这些是你的积木
(?=.*[a-z]) // Whatever is inside the [] meens the string contains at least one charter inside that set.
// If you wanted a minimum of three lowercase letters you can chain the inner block like so
(?=(.*[a-z]){3,})
这篇关于如何在这个“密码复杂性"正则表达式中允许下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!