Advertisement

Pattern library / Validation

Regex to Validate a Strong Password

A password policy is five checks wearing one disguise: at least one lowercase letter, one uppercase letter, one digit, one special character and a minimum length. Regex can hold all five at once because lookaheads are zero-width — ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$ runs four assertions that consume nothing and then lets .{8,} do the length. Every assertion starts at the same place, the ^, and scans forward through the whole password, while the trailing $ is what stops .{8,} from matching a prefix and walking away: drop it and Ab1xyz!9garbage validates as "strong". The pattern runs in JavaScript, Python re and PCRE, but it is not RE2-safe — RE2 has no lookahead, so GA4 and BigQuery reject it before they read a single character (why lookahead is off-limits in RE2, plus the four-rule workaround in the BigQuery section below). It also validates shape, not strength: Password1! passes every assertion here and sits in every breach list, so treat it as your form rule and back it with a 12+ length floor and a common-password denylist. Run it live in the tester, and see username rules, strict email validation and word-list matching for the neighbouring form fields.

The pattern
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
Tr0ub4dor&3Matchlower, upper, digit, & and 11 charactersTry →
S3cret!RoomMatchall four classes present, 11 charactersTry →
Ab1xyz!9Matchboundary — exactly 8 characters still passesTry →
Password1No matchno special characterTry →
Password 1No matcha space is not a special character — the class excludes whitespaceTry →
Ab1xyz!No matchonly 7 characters — length assertion failsTry →

Where to use it

JavaScript

const STRONG = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/u;

STRONG.test('Tr0ub4dor&3');   // true
STRONG.test('Ab1xyz!9');      // true  — exactly 8 characters
STRONG.test('Password1');     // false — no special character
STRONG.test('Password 1');    // false — a space is not a special character
STRONG.test('Ab1xyz!');       // false — only 7 characters

Python

import re

STRONG = re.compile(r"""^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$""")

STRONG.search('Tr0ub4dor&3')   # <re.Match: span=(0, 11)>
STRONG.search('Password1')     # None — no special character
STRONG.search('Password 1')    # None — a space is not a special character

# \d is Unicode-aware in Python: re.search(r'\d', '١٢٣') matches.
# Use [0-9] above if your rule means an ASCII digit.

BigQuery / GA4 (RE2)

-- RE2 has no lookahead, so the rule cannot be ported as one pattern.
-- Split it: four REGEXP_CONTAINS calls plus a length check.
SELECT
  LENGTH(pw) >= 8
  AND REGEXP_CONTAINS(pw, r'[a-z]')
  AND REGEXP_CONTAINS(pw, r'[A-Z]')
  AND REGEXP_CONTAINS(pw, r'[0-9]')
  AND REGEXP_CONTAINS(pw, r'[!@#$%^&*_+=\-]')   -- widen to your full symbol set
  AS is_strong
FROM (SELECT 'Tr0ub4dor&3' AS pw);
-- is_strong = TRUE

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

Why does this pattern throw SyntaxError in GA4 or BigQuery?

Because RE2 implements neither lookahead nor backreferences, and GA4 and BigQuery both run RE2 — the pattern cannot be parsed at all, so there is no "works, just slower" version (RE2 vs PCRE). Use the split form from the BigQuery section above: four REGEXP_CONTAINS calls plus a length check. It is also better UX, because you can tell the user exactly which rule they failed.

How do I require two digits, or ban repeated characters?

Add another zero-width assertion: (?=.*\d.*\d) requires two digits, (?=.*[!@#]) narrows the symbol set, and (?!(.)\1\1) bans any character three times in a row. Keep every rule as its own lookahead so the length check stays a single .{8,} — but \1 is a backreference, so that last one is JavaScript/PCRE only as well.

Should I use this regex to confirm a password twice on a signup form?

No. Comparing two fields is equality, not a pattern: write confirm === password in JavaScript and if (password !== confirm) on the server. Run this pattern against each field on its own and show a checklist of which rules passed — one boolean from one pattern tells the user nothing about what to fix.

Username Regex — Validate 3–20 Char Usernames · Strict Email Regex — RFC-ish Validation — Test & Copy · Regex to Match Any Word from a List — Test & Copy

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.