Solution Review: Password Checker

Solution to RegEx exercise in JavaScript

We'll cover the following

Solution

The solution requires a series of RegExes to test each condition so that when all conditions pass, we return True or otherwise False. Use the test method to conveniently check if a string matches with a certain RegEx.

For each condition, use the following RegExes.

  • Length: .{6,} (Minimum 6 characters)
  • Uppercase letter: .*[A-Z] (A-Z)
  • Lowercase letter: .*[a-z] (a-z)
  • Numeric digit: .*\d (0-9)
  • Special Character: .*[\W] (any character other than {0-9, a-z, A-Z})

Then use a series of lookaheads to join these RegExes together after anchoring them to the start and end of the string, using ^ and $ respectively.

Final RegEx:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W]).{6,}$/

Looking at the above RegEx, use a series of lookaheads (?=...) to check for each condition after the start of the string (^...). Leave the last condition out of the lookahead .{6,} and follow it by the end of the string positional quantifier $.

Look below to see the code in action.

Get hands-on with 1200+ tech skills courses.