javascript 用于检查 4 个不同字符组中是否至少存在 3 个的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5950756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:54:57  来源:igfitidea点击:

Regex for checking that at least 3 of 4 different character groups exist

javascriptregexpasswords

提问by Yonder

I'm trying to write a password validator.

我正在尝试编写一个密码验证器。

How can I see if my supplied string contains at least 3 different character groups?

如何查看我提供的字符串是否包含至少 3 个不同的字符组?

It's easy enough to check if they are existant or not ---but at least 3?

检查它们是否存在很容易——但至少有 3 个?

  • at least eight (8) characters

  • At least three different character groups

    upper-case letter

    lower-case letter

    numeric

    special characters !@#$%&/=?_.,:;-\

  • 至少八 (8) 个字符

  • 至少三个不同的字符组

    大写字母

    小写字母

    数字

    特殊字符 !@#$%&/=?_.,:;-\

(I'm using javascript for regex)

(我正在使用 javascript 进行正则表达式)

回答by Bart Kiers

Just to learn - would this kind of requirement be possible to implement in pure regex?

只是为了学习 - 这种要求可以在纯正则表达式中实现吗?

That'd make it a rather hard to read (and therefor maintain!) solution, but here it is:

这将使它成为一个相当难以阅读(并因此维护!)的解决方案,但它是:

(?mx)
^
(
  (?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])                # must contain a-z, A-Z and 0-9
  |                                                # OR
  (?=.*[a-z])(?=.*[A-Z])(?=.*[!@\#$%&/=?_.,:;\-]) # must contain a-z, A-Z and special
  |                                                # OR
  (?=.*[a-z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\-]) # must contain a-z, 0-9 and special
  |                                                # OR
  (?=.*[A-Z])(?=.*[0-9])(?=.*[!@\#$%&/=?_.,:;\-]) # must contain A-Z, 0-9 and special
)
.{8,}                                              # at least 8 chars
$

A (horrible) Javascript demo:

一个(可怕的)Javascript 演示:

var pw = "aa$aa1aa";
if(pw.match(/^((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&\/=?_.,:;\-])|(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%&\/=?_.,:;\-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%&\/=?_.,:;\-])).{8,}$/)) {
  print('Okay!');
} else {
  print('Fail...');
}

prints: Okay!, as you can see on Ideone.

打印:Okay!,正如您在Ideone 上看到的那样

回答by Brad Christie

May as well join in on the fun:

不妨一起来凑热闹:

String.prototype.isValidPW = function(){   
    // First, check for at least 8 characters
    if (this.length < 8) return false;

    // next, check that we have at least 3 matches
    var re = [/\d/, /[A-Z]/, /[a-z]/, /[!@#$%&\/=?_.,:;-]/], m = 0;
    for (var r = 0; r < re.length; r++){
        if ((this.match(re[r]) || []).length > 0) m++;
    }

    return m >= 3;
};

if ("P@ssW0rd".isValidPW()) alert('Acceptable!');

Demo

演示

回答by Kshitij Saxena -KJ-

I am assuming that you will be using different regexes for different requirements. In that case, tell me if the following work for you:

我假设您将针对不同的要求使用不同的正则表达式。在这种情况下,请告诉我以下是否适合您:

var e = password.match(/.{8,}/); //At least 8 chars

var a = password.match(/[0-9]+/); //numeric
var b = password.match(/[A-Z]+/); //Capitals
var c = password.match(/[a-z]+/); //small letters
var d = password.match(/[!@#$%&/=?_.,:;-\]+/); //special chars

if (a + b + c + d > 2 && e) {// Success}
else {// Failure}

回答by gaRex

http://jsfiddle.net/aSsR8/6/

http://jsfiddle.net/aSsR8/6/

/**
 * Function determine, wheter we have valid password
 * 
 * @param {String} value
 * @return {Boolean}
 */
function isValidPassword(value) {
    // Here we define all our params
    var validLength = 8,
        minSuccess  = 3,
        isNumeric   = + /\d+/.test(value),
        isCapitals  = + /[A-Z]+/.test(value),
        isSmall     = + /[a-z]+/.test(value),
        isSpecial   = + /[!@#$%&\/=\?_\.,:;\-]+/.test(value);

    if (value.length < validLength) { // 8 symbols. We don`t need regexp here
        return false;
    }

    if (isNumeric + isCapitals  + isSmall + isSpecial < minSuccess) {
        return false;
    }

    return true;
}


document.writeln(isValidPassword('abc'));
document.writeln(isValidPassword('abc123ABC'));
document.writeln(isValidPassword('abc123!23'));

回答by Bryce Siedschlaw

Crimson's answer didn't work for me. Here is what I have.

Crimson 的回答对我不起作用。这是我所拥有的。

var mystring = 'bLahbla\';
var valid_char_count = 0;
var lc = mystring.match(/[a-z]+/);
var uc = mystring.match(/[A-Z]+/);
var dc = mystring.match(/[0-9]+/);
var sc = mystring.match(/[\!\@\#$\%\&\=\?\_\.\,\:\;\-\]/);

if( lc ){ valid_char_count++; }
if( uc ){ valid_char_count++; }
if( dc ){ valid_char_count++; }
if( sc ){ valid_char_count++; }

if( valid_char_count >= 3 ){ /* success */ }

回答by herostwist

This will do all that in one regex

这将在一个正则表达式中完成所有这些

^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W])(?=.*[!@#\$%&/=?_\.,:;-\\]).*$

^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W])(?=.*[!@#\$%&/=?_\.,:;-\\]).*$