Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: start of string | Starts matching from the beginning |
(?=.*[a-z]) |
Positive lookahead: ensures at least one lowercase letter exists anywhere in the string | abc123ABC |
(?=.*[A-Z]) |
Positive lookahead: ensures at least one uppercase letter exists anywhere in the string | abc123ABC |
(?=.*\d) |
Positive lookahead: ensures at least one digit exists anywhere in the string | abc123ABC |
[A-Za-z\d@$!%*?&]{8,} |
Character class matching any of the specified characters, repeated 8 or more times | aA1@$!%b |
$ |
Anchor: end of string | Ensures no extra characters follow |
Matches
Password1Secure@123Abcdefg1A1b2C3d4Test@1234
Does not match
passwordPASSWORD112345678Pass word1Pass@123short1APass@123$
Common pitfalls
- Forgetting that lookaheads are zero-width assertions and do not consume characters.
- Assuming the pattern allows spaces or other characters not in the character class.
- Not realizing that the lookaheads check the entire string, not just the start.
- Overlooking that the character class does not include all special characters by default.
Performance notes
The pattern is efficient for typical use cases but may cause catastrophic backtracking if the input string is very long and does not match early. The anchors (^ and $) help limit the scope. Consider pre-filtering input length before applying this regex.