如何检查字符串是否至少包含一个数字、字母和既不是数字也不是字母的字符?

How do I check if a string contains at least one number, letter, and character that is neither a number or letter?

语言是javascript。

将通过的字符串:

JavaScript1*

Pu54 325

()9c

不会通过的字符串:

654fff

%^(dFE

我尝试了以下方法:

var matches = password.match(/\d+/g);
if(matches != null)
 {
        //password contains a number
        //check to see if string contains a letter
        if(password.match(/[a-z]/i))
        {
            //string contains a letter and a number
        }
 }

您可以使用正则表达式:

我是从这里拿来的:Regex for Password

var checkPassword = function(password){
    return !!password.match(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #+=\(\)\^?&])[A-Za-z\d$@$!%* #+=\(\)\^?&]{3,}$/);
};

我使用这个正则表达式:

至少 3 个字符至少 1 个字母、1 个数字和 1 个特殊字符:

"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #=+\(\)\^?&])[A-Za-z\d$@$!%* #=+\(\)\^?&]{3,}$"

此正则表达式将强制执行这些规则:

至少一个英文字母,(?=.*?[A-Za-z])

至少一位数,(?=.*\d)

至少一个特殊字符, (?=.[$@$!% #+=()\^?&]) 喜欢的可以加...

最少3个字符的长度(?=.[$@$!%#?&])[A-Za-z\d$@$!%*#+= ()\^?&]{3,}包含空格

如果你想添加更多的特殊字符,你可以像我添加'('一样添加到正则表达式中(你需要在两个地方添加它)。

对于那些问自己那两个感叹号是什么的人,这里是答案:What is the !! (not not) operator in JavaScript?