Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active August 29, 2015 13:56
Show Gist options
  • Save Chryus/8843341 to your computer and use it in GitHub Desktop.
Save Chryus/8843341 to your computer and use it in GitHub Desktop.
An example that uses lookarounds in Javascript to validate a password. Example from https://www.codewars.com/.
function validate(password) {
return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-z]{6,}$/.test(password);
}
//If password contains at least one digit, one uppercase char, one lowercase char PLUS six or more
//chars from the character class composed of one digit, one uppercase char, and one lowercase char.
> validate('djI38D55');
true
//passes if 6 or more chars
> validate('djI38D55666');
true
fails if no digits persent
> validate('djiDdF');
false
//fails if no lowercase chars present
> validate('DJI38D55');
false
// fails if no uppercase chars present
> validate('dji38d55');
false
//fails if less than 6 chars
> validate('djI38');
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment