连续使用相同字符组合密码正则表达式

时间:2015-04-27 05:10:08

标签: java javascript regex

我有一个密码正则表达式

^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*])).{0,100}$

我想添加将验证此功能的正则表达式,并且还可以排除连续插入四个相同的字符

我发现这个模式会匹配4个相同的字符,但我如何组合它们,这个模式应该检查有4个相同的字符密码不应该有效。

(。)\ 1 {3}

1 个答案:

答案 0 :(得分:1)

这有效:

^((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*]))((.)(?!\3{3})){0,100}$

说明:

^                     // beginning of string
(                     // begin capturing group #1 (this is actually unnecessary)
  (?=.*\d)            // must contain a digit
  (?=.*[a-z])         // must contain a lowercase letter
  (?=.*[A-Z])         // must contain an uppercase letter
  (?=.*[!@#$%&*])     // must contain a special character
)                     // end capturing group #1
(                     // begin capturing group #2: one character of password, for repetition quantifier
  (.)                 // capturing group #3: one character of password, for negative lookahead
  (?!\3{3})           // character is not followed by itself 3 times
)                     // end capturing group #2
{0,100}               // repeat group #2 up to 100 times
$                     // end of string