← Back to Toolbox

Regex Cheat Sheet

Updated October 2026 · Bookmark it for later — once you've written a pattern, verify it in the online regex tester

Character Matching

PatternMeaning
.Any character except a newline
\d / \DDigit / non-digit
\w / \WLetter, digit or underscore / the opposite
\s / \SWhitespace character / non-whitespace
[abc]Any one of a, b or c
[^abc]Any character except a, b or c
[0-9] / [a-z]Character range

Quantifiers

PatternMeaning
*0 or more times
+1 or more times
?0 or 1 time
{n} / {n,} / {n,m}Exactly n times / at least n times / between n and m times
*? / +?Non-greedy (match as few as possible)

Anchors and Groups

PatternMeaning
^ / $Start / end of string
\bWord boundary
(abc)Capturing group — reference it with $1
(?:abc)Non-capturing group
a|bAlternation (or)
(?=abc) / (?!abc)Positive lookahead / negative lookahead

Common Examples (Copy & Paste)

Use casePattern
Mobile number (mainland China)^1[3-9]\d{9}$
Email^[\w.-]+@[\w.-]+\.\w+$
URL^https?://[\w.-]+(:\d+)?(/.*)?$
Chinese ID number (18 digits)^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$
IPv4^(\d{1,3}\.){3}\d{1,3}$
Date YYYY-MM-DD^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Password (8+ chars, letters and digits)^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
Trim leading/trailing whitespace^\s+|\s+$ (replace with an empty string)

Watch Out For

1. Don't chase the "perfect regex" for email/URL. A spec-complete email regex runs to thousands of characters — good enough is good enough; the real validation is sending a verification email.
2. Beware of catastrophic backtracking: nested quantifiers like (a+)+$ against a long string can hang forever. Add concrete bounds or switch to non-greedy matching.
3. Before splicing user input into a regex, run it through an escape function for special characters — never concatenate it raw.

Written one? Come and test it

Open the Regex Tester →