^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W])[A-Za-z\d\W]{8,}
密码验证的正则表达式应至少8个字符,1个小写,1个大写,1个数字,1个特殊字符,不应包含空格和逗号
答案 0 :(得分:1)
您可以创建包含RegExp
项的数组,这些项应返回true
,哪些应返回false
,使用Array.prototype.every()
检查字符串是否包含每个值
var str = `1aB$1234`;
var str1 = `1aB$,234`;
var str2 = `1aB$123 `;
// should match at least one of each `RegExp` in array
var matches = [/[a-z]{1}/, /[A-Z]{1}/, /[0-9]{1}/, /[!#$]{1}/];
// should not match any `RegExp` in array
var not = [/,/, /\s/]
var checkpw = (input) => matches.every(match => new RegExp(match).test(input))
&& not.every(n => !new RegExp(n).test(input))
&& input.length === 8;
console.log("\n","str:" + `${str}:` + checkpw(str) + "\n"
, "str1:" + `${str1}:` + checkpw(str1) + "\n"
, "str2:" + `${str2}:` + checkpw(str2));