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.
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$
Load this pattern into the tester →
How the pattern works
- Each
(?=.*...)is zero-width: it looks ahead for its character class and consumes nothing, so all four assertions run against the same password before.{8,}measures length. That trick is also why the pattern is not RE2-safe — RE2, and therefore GA4 and BigQuery, has no lookahead at all, so the pattern will not even parse (RE2 vs PCRE). JavaScript, Pythonre, Java, .NET and PCRE all support it. - Both anchors earn their place.
^pins every lookahead to the start of the string so each one scans the entire password;$is what stops.{8,}from matching the first eight characters and ignoring the rest. Without itAb1xyz!9garbagepasses as a strong password. - The symbol class is an explicit list of printable ASCII punctuation, not "any non-word character". It deliberately excludes whitespace, so a space never satisfies the special-character rule (example 5), and it also excludes
é,ñand€— which is whyCafé1234fails. Edit the class if your policy accepts other characters. \dand[A-Z]are ASCII-only in JavaScript, with or without flags. Python'sredisagrees:re.search(r'\d', '١٢٣')matches an Arabic-Indic digit, so(?=.*\d)can be satisfied without a single 0–9. When a rule means ASCII, write it ASCII —[0-9]behaves identically in every engine.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
Tr0ub4dor&3 | Match | lower, upper, digit, & and 11 characters | Try → |
S3cret!Room | Match | all four classes present, 11 characters | Try → |
Ab1xyz!9 | Match | boundary — exactly 8 characters still passes | Try → |
Password1 | No match | no special character | Try → |
Password 1 | No match | a space is not a special character — the class excludes whitespace | Try → |
Ab1xyz! | No match | only 7 characters — length assertion fails | Try → |
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
- RE2 has no lookahead, so this pattern does not merely slow down in GA4 or BigQuery — it fails to parse (
SyntaxErrorin JS, Invalid argument in BigQuery). There is no partial port. The RE2-safe version is four rules plus a length check: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 because it is split, you can report which rule failed instead of one useless boolean. - The
m(multiline) flag turns^and$into line anchors, so each line of a pasted string is validated on its own:"aaaaaaaa\nAb1xyz!9"passes under/gmwhile the password as a whole is nonsense. JavaScript has no\A/\z, so reject newlines first —if (/[\r\n]/.test(pw)) reject— or strip them before testing. - A pass here means "meets the policy", not "is hard to guess":
Password1!andSummer2026!satisfy every assertion and both appear in every breach corpus. Length dominates everything else (12+ for anything that matters), so keep this pattern as form feedback, back it with a common-password denylist such as the ones checked against HIBP, and never log what it accepted.
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.
Related patterns
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.