【问题标题】:Regular expression to validate a string for at least n number of Upper case, Lower case , number and special characters in a given string正则表达式,用于验证给定字符串中至少 n 个大写、小写、数字和特殊字符的字符串
【发布时间】:2016-09-16 04:19:25
【问题描述】:

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

我找到了这么多解决方案,可以为至少 1 个特殊/大写/小写/数字 (Regex for Password: "Atleast 1 letter, 1 number, 1 special character and SHOULD NOT start with a special character") 提供解决方案,但没有通用解决方案可以满足我的要求。

我在下面尝试了我的字符串中至少 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

【问题讨论】:

  • 好吧,您应该在构造函数表示法中使用双反斜杠,并且您不应该将"g" 标志与RegExp.test() 方法中使用的正则表达式一起使用。试试var PasswordRegEx =/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+]{n})[A-Za-z\d!@#$%^&*()_+]{8,20}/;
  • “构造函数符号中的双反斜杠”是什么意思?
  • new RegExp(Pattern, flags) - 这是构造函数符号。您需要使用另一个反斜杠来转义反斜杠,因此需要使用双反斜杠。
  • 您使用字符串初始化 RegExp。因此,里面的所有反斜杠都用作转义符号。正则表达式引擎在 d 之前需要一个文字 \ 才能实际匹配一个数字。只需尝试提醒您的PasswordPattern,您就会明白我的意思。
  • (?=[^a-z]*[a-z]) 至少一个字母并不复杂,(?=(?:[^a-z]*[a-z]){2}) 两个字母等等。

标签: javascript jquery regex


【解决方案1】:

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

n = 2 构造正则表达式:

var n = 2;
var constructedRegEx = "^(?=(?:.*[0-9]){" + n + ",})(?=(?:.*[a-z]){" + n + ",})(?=(?:.*[A-Z]){" + 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
----------------------------------------------------------------------

Regex101

【讨论】:

  • @JitendraPancholi 不客气。我在等你再次发布你的问题,所以我有一个先机。 :)
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2011-01-22
  • 2021-09-08
相关资源
最近更新 更多