Regex Cheatsheet is a free interactive reference for regular expressions. Look up character classes, quantifiers, anchors, groups, and lookaround assertions with live examples you can test in the browser. Covers JavaScript, Python, and common validation patterns.
Live Regex Tester
Reference
| Pattern | Description | Example | |
|---|---|---|---|
| . | Any character except newline | a.c → "abc", "a1c" | |
| \d | Digit [0-9] | \d\d → "42", "99" | |
| \D | Non-digit | \D+ → "abc" | |
| \w | Word char [a-zA-Z0-9_] | \w+ → "hello_123" | |
| \W | Non-word char | \W → "!", "@" | |
| \s | Whitespace | \s+ → " ", "\t" | |
| \S | Non-whitespace | \S+ → "hello" | |
| [abc] | Any of a, b, or c | [aeiou] → vowels | |
| [^abc] | Not a, b, or c | [^aeiou] → consonants | |
| [a-z] | Range a to z | [a-z]+ → lowercase |
? for lazy matching.
| Pattern | Description | Example | |
|---|---|---|---|
| * | 0 or more | ab*c → "ac", "abc", "abbc" | |
| + | 1 or more | ab+c → "abc", "abbc" | |
| ? | 0 or 1 | colou?r → "color", "colour" | |
| {n} | Exactly n times | \d{4} → "2024" | |
| {n,} | n or more times | \d{2,} → "42", "123" | |
| {n,m} | Between n and m times | \d{2,4} → "42", "123" | |
| *? | 0 or more (lazy) | <.*?> → shortest match | |
| +? | 1 or more (lazy) | \".+?\" → shortest string |
| Pattern | Description | Example | |
|---|---|---|---|
| ^ | Start of string/line | ^Hello → starts with "Hello" | |
| $ | End of string/line | World$ → ends with "World" | |
| \b | Word boundary | \bcat\b → "cat" not "cats" | |
| \B | Non-word boundary | \Bcat → "cats" not "cat" |
| Pattern | Description | Example | |
|---|---|---|---|
| (abc) | Capturing group | (\d+)-(\d+) → captures parts | |
| (?:abc) | Non-capturing group | (?:Mr|Mrs) Smith | |
| \1, \2 | Backreference | (\w)\1 → doubled letters | |
| (?<name>abc) | Named capturing group | (?<year>\d{4}) | |
| a|b | Alternation (or) | cat|dog → "cat" or "dog" |
| Pattern | Description | Example | |
|---|---|---|---|
| (?=abc) | Positive lookahead | \d+(?=px) → number before "px" | |
| (?!abc) | Negative lookahead | \d+(?!px) → not before "px" | |
| (?<=abc) | Positive lookbehind | (?<=\$)\d+ → after "$" | |
| (?<!abc) | Negative lookbehind | (?<!\$)\d+ → not after "$" |
| Use Case | Pattern | |
|---|---|---|
| Email (simple) | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} | |
| URL | https?://[^\s]+ | |
| Phone (US) | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} | |
| IP Address (IPv4) | \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b | |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} | |
| Time (HH:MM) | \d{1,2}:\d{2}(?::\d{2})? | |
| Hex Color | #[0-9A-Fa-f]{3,6}\b | |
| Username | ^[a-zA-Z][a-zA-Z0-9_]{2,15}$ | |
| Strong Password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ | |
| Slug | ^[a-z0-9]+(?:-[a-z0-9]+)*$ |
JavaScript
/pattern/flags
str.match(regex)
str.replace(regex, new)
regex.test(str)
Python
import re
re.search(pattern, str)
re.findall(pattern, str)
re.sub(pattern, new, str)
Flags
g - global (all matches)
i - case insensitive
m - multiline (^ $ per line)
s - dotall (. matches \n)
About This Regex Cheatsheet
Regular expressions are a pattern language built into virtually every programming language and text editor. They let you describe the shape of text — "three digits followed by a dash" or "a word at the start of a line" — so a computer can find, extract, or replace matching fragments automatically.
This cheatsheet covers six categories of regex syntax: character classes (\d, \w, [a-z]), quantifiers (*, +, {n,m}), anchors (^, $, \b), groups and backreferences, lookaround assertions, and ready-to-use patterns for common tasks like email, URL, and date validation.
Every pattern in the reference table has a Try button that loads it into the live tester above so you can see matches highlighted in real time. You can also modify the pattern, toggle flags, and paste your own test strings.
Worked Examples
1. Extract all prices from a receipt.
Pattern: \$\d+\.\d{2}
Test string: Coffee $4.50, Bagel $3.25, Tax $0.62
Result: 3 matches — $4.50, $3.25, $0.62
2. Validate a date in YYYY-MM-DD format.
Pattern: ^\d{4}-\d{2}-\d{2}$
Test string: 2024-01-15 → match. 15/01/2024 → no match.
This pattern checks structure only. To verify that month ≤ 12 and day ≤ 31, add application logic.
3. Replace duplicate whitespace.
Pattern: \s{2,} with replacement (single space).
Input: hello world → Output: hello world.
In JavaScript: str.replace(/\s{2,}/g, ' ')
When to Use Regex
- Search and replace — find patterns in log files, code, or documents
- Input validation — check format of emails, phone numbers, dates, or codes
- Data extraction — pull structured pieces (prices, IDs, timestamps) from unstructured text
- Text cleanup — normalize whitespace, strip HTML tags, fix formatting
Limitations
Regex cannot parse nested structures like HTML or JSON reliably. For those, use a proper parser. Regex engines also differ between languages — JavaScript lacks some features available in Python or Perl (e.g., atomic groups, conditional patterns). Always test patterns against edge cases before deploying to production.
Frequently Asked Questions
Privacy
All regex testing runs locally in your browser using JavaScript. No patterns, test strings, or match results leave your device. Nothing is logged, stored, or sent to any server. There is no account, no tracking, and no cookies.