开发者

Regular Expression matching at least n of m groups

I was going to write a regular expression that would match only if a string contains at least n different classes开发者_开发知识库 of characters. I was going to use this to force my users to create strong passwords and wanted to check if the password contains at least 3 of the following:

  • Characters
  • Capital Characters
  • Numbers
  • Special Characters

Writing a regular expression that matches if all of those classes are present is trivial using lookaheads. However, I cannot wrap my head around the "at least 3" part. Is this even possible (in a nice, compact expression) or would I have to create a monster expression?


I think this will be more compact than listing each possible combination of 3 of the 4. It utilizes negative lookahead to make sure that the entire string is not composed of only one or two of the character classes you listed:

(?!([a-zA-Z]*|[a-z\d]*|[^A-Z\d]*|[A-Z\d]*|[^a-z\d]*|[^a-zA-Z]*)$).*

In order, the groups here are:

  • lower and/or upper
  • lower and/or digits
  • lower and/or special
  • upper and/or digits
  • upper and/or special
  • digits and/or special

This regex will fail if the entire string (because of the $ in the negative lookahead) contains only characters from any of the above groups.


You have to write an expression for each possible combination of 3 of the 4 (four expressions in total), and then | the individual expressions together so that they pass if they fulfill at least one of the original expressions.


What do you think about this solution?

var str='23khkS_s';

var countGroups=0;
if(str.match(/[a-z]+/))
    countGroups++;
if(str.match(/[A-Z]+/))
    countGroups++;
if(str.match(/[0-9]+/))
    countGroups++;
if(str.match(/[-_!@#$%*\(\)]+/))
    countGroups++;

console.log(countGroups);

Instead of using 1 monster-like expression you can use 4 small RE. And then work with variable countGroups, which contains number of maеched character groups. I hope it is helpfull

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜