Qaynaq LABS · TOOLS

REGEX · JAVASCRIPT

Regex explained

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

Matches strings that are at least 8 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 position at start of the string Start of 'Aa1@abc'
(?=.*[a-z]) Positive lookahead: ensures at least one lowercase letter exists anywhere in the string Lookahead passes for 'a' in 'Aa1@abc'
(?=.*[A-Z]) Positive lookahead: ensures at least one uppercase letter exists anywhere in the string Lookahead passes for 'A' in 'Aa1@abc'
(?=.*\d) Positive lookahead: ensures at least one digit exists anywhere in the string Lookahead passes for '1' in 'Aa1@abc'
(?=.*[@$!%*?&]) Positive lookahead: ensures at least one special character from the set exists anywhere in the string Lookahead passes for '@' in 'Aa1@abc'
[A-Za-z\d@$!%*?&]{8,} Character class matching any letter, digit, or specified special character, repeated 8 or more times Matches 'Aa1@abc' (8 chars)
$ Anchor: asserts position at end of the string End of 'Aa1@abc'

Matches

  • Aa1@abc
  • Password123!
  • Zx9@qwerty
  • Secure@1

Does not match

  • password
  • PASSWORD
  • 12345678
  • Pass1234
  • Aa1@ab
  • Aa1@abcdefghijklmnopqrstuvwxyz

Common pitfalls

  • Forgetting to escape special regex characters like $ in the character class (must be \$ in JavaScript regex literals).
  • Assuming the lookaheads check for characters in order rather than anywhere in the string.
  • Not anchoring the entire pattern (^ and $) leading to partial matches in longer strings.
  • Overlooking that the character class includes only the specified special characters; others like # or ^ will fail.

Performance notes

The pattern is efficient for typical use cases as lookaheads are evaluated in linear time relative to string length. However, very long strings with many characters could slow down lookahead checks. The {8,} quantifier ensures the main pattern matches only strings of sufficient length, avoiding unnecessary checks on short strings.

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 the special characters not escaped in the lookahead but are in the character class?

In the lookahead (?=.*[@$!%*?&]), the characters $, *, ?, and ! are treated as literals because they are inside a character class [...]. In the main character class [A-Za-z\d@$!%*?&], $ must be escaped as \$ to avoid being interpreted as an anchor, while other special characters are safe as literals within the class.

Can this pattern be simplified to reduce lookaheads?

No, the four separate lookaheads are necessary to enforce the presence of each required character type independently. Combining them into a single lookahead would not guarantee all types are present.

Does this pattern work for Unicode characters like accented letters?

No, the pattern only matches ASCII letters (A-Za-z). For Unicode support, you would need to use flags like 'u' and adjust character classes (e.g., [\p{Ll}] for lowercase letters).