Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,32}$

A regex that enforces passwords to be 12-32 characters long and contain at least one lowercase letter, one uppercase letter, one digit, and one special character from the set @$!%*?&.

Token-by-token breakdown

TokenMeaningExample
^ 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@ssw0rd123
  • MyP@ssw0rdIsLongEnough
  • aA1@aaaaaaaaaaa
  • Zz9!zzzzzzzzzzzzz

Does not match

  • short1@A
  • NoSpecialChar123
  • nouppercase123@
  • NOLOWERCASE123@
  • MissingDigit@Abc
  • 12345678901234
  • !@#$%^&*()!@#$%
  • 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.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Why are there four separate lookaheads instead of one combined lookahead?

Separate lookaheads allow independent checks for each required character type (lowercase, uppercase, digit, special). Combining them into one would complicate the pattern and reduce readability without performance benefits.

Can this regex be used for passwords longer than 32 characters?

No. The final character class {12,32} explicitly limits the password length to 12-32 characters.

Is the special character set @$!%*?& exhaustive for common password requirements?

No. The set is limited to these characters. If other special characters are required (e.g., #, ^, &), they must be added to the character class.

Does this regex allow spaces in passwords?

No. The final character class [A-Za-z\d@$!%*?&] does not include the space character. If spaces are allowed, they must be added to the class.

How can I modify this regex to require exactly one special character?

Replace the final character class with a pattern that enforces exactly one special character, e.g., (?=(.*[@$!%*?&]){1})[A-Za-z\d]{11,31}[@$!%*?&][A-Za-z\d]{0,31}. This is more complex and may impact performance.