Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vigosan/71133bc77554c6c66b3396ce88aee958 to your computer and use it in GitHub Desktop.
Save vigosan/71133bc77554c6c66b3396ce88aee958 to your computer and use it in GitHub Desktop.
Example of using regex to check a complex password validation requirement ("use at least 1 character from 3 sets of characters out of a total of 4 sets of characters")
PASSWORD_VALIDATOR = /( # Start of group
(?: # Start of nonmatching group, 4 possible solutions
(?=.*[a-z]) # Must contain one lowercase character
(?=.*[A-Z]) # Must contain one uppercase character
(?=.*\W) # Must contain one non-word character or symbol
| # or...
(?=.*\d) # Must contain one digit from 0-9
(?=.*[A-Z]) # Must contain one uppercase character
(?=.*\W) # Must contain one non-word character or symbol
| # or...
(?=.*\d) # Must contain one digit from 0-9
(?=.*[a-z]) # Must contain one lowercase character
(?=.*\W) # Must contain one non-word character or symbol
| # or...
(?=.*\d) # Must contain one digit from 0-9
(?=.*[a-z]) # Must contain one lowercase character
(?=.*[A-Z]) # Must contain one uppercase character
) # End of nonmatching group with possible solutions
.* # Match anything with previous condition checking
)/x # End of group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment