Lookahead for password rules
Stacked lookaheads let you require several things at once without dictating their order.
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/
Match a whole string consisting of a position followed by zero or more of any character, then "a" to "z", then a position followed by zero or more of any character, then "A" to "Z", then a position followed by zero or more of any character, then a digit, then 8 or more of any character.
What to check
Note
Anchors match string ends, not line ends
Without the m flag, ^ and $ match only the very start and end of the input — not the start and end of each line. On multi-line input this is often not what was intended.
Fix: Add the m flag if the pattern should apply per line.
Step by step
^Anchor to the start of the input.
(?=.*[a-z])Match a position followed by zero or more of any character, then "a" to "z".
(?=.*[A-Z])Match a position followed by zero or more of any character, then "A" to "Z".
(?=.*\d)Match a position followed by zero or more of any character, then a digit.
.{8,}Match 8 or more of any character.
$Anchor to the end of the input.
Details
- Capture groups
- None
- Flags
- None set
Check your own pattern
Paste one on the home page, or call the API or MCP server. This page is also available as markdown.