0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.