Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the position at the start of the string | Start of string |
(?=.*[a-z]) |
Positive lookahead: ensures at least one lowercase letter exists anywhere in the string | a1B@... or 1aB@... |
(?=.*[A-Z]) |
Positive lookahead: ensures at least one uppercase letter exists anywhere in the string | A1b@... or 1Ab@... |
(?=.*\d) |
Positive lookahead: ensures at least one digit exists anywhere in the string | 1aB@... or a1B@... |
(?=.*[@$!%*?&]) |
Positive lookahead: ensures at least one special character from the set exists anywhere in the string | @aB1... or a@B1... |
[A-Za-z\d@$!%*?&]{12,32} |
Character class matching any alphanumeric or special character in the set, repeated 12 to 32 times | aB1@... (12-32 chars) |
$ |
Anchor: asserts the position at the end of the string | End of string |
Matches
Abcdefg12345@SecureP@ssw0rd123MyP@ssw0rdIsLongEnoughaA1@aaaaaaaaaaaZz9!zzzzzzzzzzzzz
Does not match
short1@ANoSpecialChar123nouppercase123@NOLOWERCASE123@MissingDigit@Abc12345678901234!@#$%^&*()!@#$%aA1@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Common pitfalls
- Forgetting to escape special regex characters like $ in the character class (though not needed in JS character classes).
- Assuming the lookaheads check the entire string when they only check for existence anywhere in the string.
- Overlooking that the final character class allows any combination of the specified characters, not just one of each type.
- Not accounting for the minimum and maximum length constraints (12-32 characters).
- Confusing the order of lookaheads or misplacing anchors.
Performance notes
The regex uses four lookaheads, which are efficient in JavaScript as they do not cause catastrophic backtracking. However, long strings (close to 32 chars) may slow down matching slightly. Anchoring at start (^) and end ($) ensures full-string validation.