Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: start of string | Start of input |
(?=.*[a-z]) |
Positive lookahead: ensures at least one lowercase letter exists anywhere in the string | abc123!@#ABC |
(?=.*[A-Z]) |
Positive lookahead: ensures at least one uppercase letter exists anywhere in the string | abc123!@#ABC |
(?=.*\d) |
Positive lookahead: ensures at least one digit exists anywhere in the string | abc123!@#ABC |
(?=.*[@$!%*?&]) |
Positive lookahead: ensures at least one special character exists anywhere in the string | abc123!@#ABC |
[A-Za-z\d@$!%*?&]{12,} |
Character class: matches 12 or more characters from the set of letters (upper/lower), digits, or specified special characters | abc123!@#ABC |
$ |
Anchor: end of string | End of input |
Matches
Abcdefghijk1!Password123@SecureP@ssw0rdaB1@aB1@aB1@aB1@
Does not match
abc123!@#ABC123!@#abcABC!@#abc123ABCAbcdefghijkAbcdefghijk1Abcdefghijk1@
Common pitfalls
- Forgetting that lookaheads do not consume characters, leading to confusion about why the pattern matches longer strings.
- Assuming the pattern allows spaces or other characters not in the character class.
- Overlooking that the pattern requires all four lookaheads to pass, not just one.
- Misunderstanding that the character class includes only the specified special characters.
Performance notes
The pattern uses four lookaheads, which are efficient in JavaScript as they do not cause catastrophic backtracking. However, very long strings may still impact performance due to the {12,} quantifier. Anchoring ensures the entire string is validated.