您可以在一个单一的正则表达式与几个lookahead assertions做到这一点(在.net中):如果所有条件都满足
^(?=.*\p{Lu})(?:.*\p{Ll})(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)
将匹配。
^ # Match the start of the string
(?=.*\p{Lu}) # True if there is at least one uppercase letter ahead
(?=.*\p{Ll}) # True if there is at least one lowercase letter ahead
(?=.*\d) # True if there is at least one digit ahead
(?=.*\W) # True if there is at least one non-alnum character ahead
(?!.*(.).*\1.*\1) # True if there is no character repeated twice ahead
注意,比赛是不会消耗字符串的任何字符 - 如果你想匹配操作返回你对匹配的字符串,在正则表达式的末尾添加.*
。
在JavaScript中,您不能使用Unicode字符属性。因此,您可以使用
^(?=.*[A-Z])(?:.*[a-z])(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)
这当然只会使用ASCII字母进行验证。如果这对你没问题,没问题。你可以去扩充像[A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ]
等字符类,但你可能永远不会完成这个。在服务器端,如果你想验证产生相同的结果,你必须指定RegexOptions.ECMAScript
,所以.NET正则表达式引擎的行为就像JavaScript引擎(感谢Alan Moore注意!)。
我很难让这个模式在Rubular上工作:http://rubular.com/r/008s20R3fa – 2011-01-25 08:54:50