创建正则表达式数组 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8207066/
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
Creating Array of Regular Expressions Javascript
提问by Chris
I want to create a function that compares a password against some commonly idiotic ones, so that the user can't pick one of these, but the function I have written so far, when put between script tags, causes no javascript to be recognized (by Firebug). I assume the array creation is at fault.
我想创建一个函数,将密码与一些常见的愚蠢密码进行比较,以便用户无法选择其中一个,但是到目前为止我编写的函数,当放在脚本标签之间时,会导致无法识别任何 javascript(由萤火虫)。我认为数组创建有问题。
function unacceptable(pwd){
var unforgivable = [
/password/gi, /*g matches any occurance of sequence, i checks case insensitive*/
/12345678/g,
/8675309/g,
/[a-z]{8,}/gi,
/qwerty/gi,
/asdfg/gi,
/qazwsx/gi,
/zxcvb/gi,
/letmein/gi,
/trustno1/gi,
/omnicloud/gi,
/monkey/gi];
for (var i=0; i<unforgivable.length; i++)
if(pwd.match(unforgivable[i])) return true;
return false;
}
回答by jfriend00
You don't need the loop to test every word as you can put them all into one regular expression (separated by the |
character) and let the regex engine look for any of them all at once. You could do that like this:
您不需要循环来测试每个单词,因为您可以将它们全部放入一个正则表达式(由|
字符分隔)中,并让正则表达式引擎一次查找其中的任何一个。你可以这样做:
function unacceptable(pwd){
var unforgivable = [
"password",
"12345678",
"8675309",
"[a-z]{8,}",
"qwerty",
"asdfg",
"qazwsx",
"zxcvb",
"letmein",
"trustno1",
"omnicloud",
"monkey"
];
var re = new RegExp(unforgivable.join("|"), "i");
return re.test(pwd);
}
Working demo here: http://jsfiddle.net/jfriend00/cyVbC/
这里的工作演示:http: //jsfiddle.net/jfriend00/cyVbC/
P.S. You don't have to put all the words into an array. You could just predeclare the entire regex, but I thought putting them in the array like this made for more readable code that was easier to maintain.
PS您不必将所有单词放入数组中。您可以预先声明整个正则表达式,但我认为将它们放在这样的数组中可以使代码更具可读性,更易于维护。
It could also be this:
也可能是这样:
var unforgivable = /password|12345678|8675309|[a-z]{8,}|qwerty|asdfg|qazwsx|zxcvb|letmein|trustno1|omnicloud|monkey/i;
function unacceptable(pwd){
return unforgivable.test(pwd);
}
回答by Renato Gama
I like using Array.some
, which will stop iterating through the array as soon as one return value is true:
我喜欢使用Array.some
,它会在一个返回值为真时停止遍历数组:
function unacceptable(pwd){
return [
/password/gi,
/12345678/g,
/8675309/g,
/[a-z]{8,}/gi,
/qwerty/gi,
/asdfg/gi,
/qazwsx/gi,
/zxcvb/gi,
/letmein/gi,
/trustno1/gi,
/omnicloud/gi,
/monkey/gi
].some(function(regexp){
return regexp.test(pwd);
});
}
回答by Jools
Found this looking for something else and as no one else has mentioned it, it needs mentioning. You should not use blacklists as a means to ensure strong passwords. It's a maintenance hole and leads to more bad passwords that just aren't in your list. Enforce strong password policies instead.
发现这个是在寻找其他东西,因为没有其他人提到过,所以需要提及。您不应使用黑名单作为确保强密码的手段。这是一个维护漏洞,会导致更多错误的密码不在您的列表中。而是强制执行强密码策略。
P4ssw0rd! would pass many psuedo strong policies but it would take seconds to crack.
P4ssw0rd!将通过许多伪强政策,但需要几秒钟才能破解。
The only effective blacklist is to incldue all word lists and combination scripts used by decryption techniques, this would mean users wait minutes/hours/days to verify if their password is good enough.
唯一有效的黑名单是包括解密技术使用的所有单词列表和组合脚本,这意味着用户需要等待几分钟/几小时/几天来验证他们的密码是否足够好。
I know this doesn't answer the specific question but it does try to advice on what is and isn't effective password validation.
我知道这并没有回答具体问题,但它确实尝试就什么是有效的密码验证和无效的密码验证提出建议。
回答by switz
You have a trailing comma. You can't use a trailing comma in javascript.
你有一个尾随逗号。您不能在 javascript 中使用尾随逗号。
var unforgivable = new Array(
/password/gi, /*g matches any occurance of sequence, i checks case insensitive*/
/12345678/g,
/8675309/g,
/[a-z]{8,}/gi,
/qwerty/gi,
/asdfg/gi,
/qazwsx/gi,
/zxcvb/gi,
/letmein/gi,
/trustno1/gi,
/omnicloud/gi,
/monkey/gi
)