我知道许多编程语言,但我不知道的是Perl。如何在Javascript中实现以下Perl代码段?
my $minGroups = 3;
my %rexGroups = (
upper => qr/[A-Z]/,
lower => qr/[a-z]/,
digit => qr/[0-9]/,
other => qr/^[A-Za-z0-9]/,
);
my $nGroups = 0;
for my $re (keys %rexGroups) {
++$nGroups if ( $pass =~ m/$rexGroups{$re}/ );
}
if ( $nGroups < $minGroups ) {
# indicate error
}
# indicate success
最佳答案
基本上,您发布的代码试图将给定的字符串与different regexes
相匹配,如果它匹配3个或多个我们在reg variable
中的正则表达式,则应将success
作为输出,否则应使用failure
作为结果。
let regs = [/[A-Z]+/g,/[a-z]+/g,/\d+/g,/[^A-Za-z0-9]+/g]
let mincount = 3;
let findSuccess = (regs,str) => {
let count = 0;
regs.forEach(e => count+= e.test(str) ? 1 : 0)
count >= mincount ? console.log('successful')
: console.log('unsuccessful')
}
findSuccess(regs,'hello 123 @ Hello')
findSuccess(regs,'Hello')
关于javascript - 将此代码段从Perl转换为Javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55084270/