繁体   English   中英

正则表达式,用于验证给定字符串中至少 n 个大写、小写、数字和特殊字符的字符串

[英]Regular expression to validate a string for at least n number of Upper case, Lower case , number and special characters in a given string

我需要添加为密码中的每种类型(大写、小写、数字和特殊)字符提供最少字符(范围 0 - 9)的能力。

在此处输入图片说明

我找到了很多解决方案,这些解决方案为至少 1 个特殊/大写/小写/数字( 密码正则表达式:“至少 1 个字母,1 个数字,1 个特殊字符,不应以特殊字符开头” )提供解决方案,但没有可以满足我的要求的通用解决方案。

我在我的字符串中尝试了至少 n 个特殊字符,但它不起作用。

function CheckSpecialChars(n, NewPassword){
  var PasswordPattern ="^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+]{n})[A-Za-z\\d!@#$%^&*()_+]{8,20}$";
  var NewPassword = $('#txt').val();
  var PasswordRegEx = new RegExp(PasswordPattern, 'g');
  if (!PasswordRegEx.test(NewPassword)) {
    $('.er').html('not matched');
    return false;
  }else{
    $('.er').html('matched');
    return false;
  }
}

// if minimum 2 special characters are mandatory
Valid String: sad@j234KSS&ff // has more than 2 special chars
Invalid String: sdf#kj034950 // has less than 2 special chars

您将需要使用构造函数构造一个正则表达式,因为n是可变的。 下面是一个例子:

n = 2构造正则表达式:

 var n = 2; var constructedRegEx = "^(?=(?:.*[0-9]){" + n + ",})(?=(?:.*[az]){" + n + ",})(?=(?:.*[AZ]){" + n + ",})(?=(?:.*[[!@#$%^&*()_+]){" + n + ",}).+$"; var PasswordRegEx = new RegExp(constructedRegEx, 'm'); console.log(PasswordRegEx.test('@Al1#a2B')); console.log(PasswordRegEx.test('@Al1#a2'));

构造的正则表达式示例:

^(?=(?:.*[0-9]){2,})(?=(?:.*[a-z]){2,})(?=(?:.*[A-Z]){2,})(?=(?:.*[!@#$%^&*]){2,}).+$

铁路图:

正则表达式可视化

说明:

    NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      .*                       any character except \n (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      [!@#0^&*]                any character of: '!', '@', '#', '0',
                               '^', '&', '*'
----------------------------------------------------------------------
    ){2,}                    end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  .+                       any character except \n (1 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

正则表达式101

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM