Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

A regex that enforces a password policy requiring at least one lowercase letter, one uppercase letter, one digit, and one special character, with a minimum length of 12 characters.

Token-by-token breakdown

TokenMeaningExample
^ 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@ssw0rd
  • aB1@aB1@aB1@aB1@

Does not match

  • abc123!@#
  • ABC123!@#
  • abcABC!@#
  • abc123ABC
  • Abcdefghijk
  • Abcdefghijk1
  • Abcdefghijk1@

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.

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 does the pattern use lookaheads instead of just the character class?

The lookaheads ensure that at least one character from each required category (lowercase, uppercase, digit, special) exists in the string, while the character class defines the allowed characters and minimum length.

Can the pattern be modified to allow spaces?

Yes, by adding a space to the character class: [A-Za-z\d@$!%*?& ]{12,}. However, the lookaheads would need to be updated to include space if it should count as a special character.

Does the pattern enforce a maximum length?

No, the pattern only enforces a minimum length of 12 characters. To enforce a maximum length, you could modify the quantifier to {12,20} or similar.

Why are some special characters escaped in the character class?

In JavaScript regex, the characters $, !, %, *, ?, and & are not special within a character class, so escaping them is unnecessary. However, escaping them does not cause errors and may be done for clarity or consistency.