CXJ
CXJ

Reputation: 4449

Convert this code snippet from Perl to Javascript

I know many programming languages, but one I don't know is Perl. How would I implement the following Perl snippet in Javascript?

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

Upvotes: 0

Views: 502

Answers (1)

Code Maniac
Code Maniac

Reputation: 37755

Basically the code you posted is trying to match the given string string against different regexes and if it match 3 or more regex we have in reg variable it should give us success as output else it should give use failure as result.

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')

Upvotes: 1

Related Questions