Regular Expression Quick Reference
Regular expressions (regex) are patterns used to match character combinations in strings. This cheatsheet covers the most common regex syntax you’ll need as a developer.
Test any pattern with our Regex Tester.
Character Classes
| Pattern | Description | Example |
|---|
. | Any character (except newline) | a.c matches “abc”, “a1c” |
\d | Any digit (0-9) | \d{3} matches “123” |
\D | Any non-digit | \D+ matches “abc” |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ matches “hello_123” |
\W | Non-word character | \W matches “@”, " " |
\s | Whitespace (space, tab, newline) | \s+ matches " " |
\S | Non-whitespace | \S+ matches “hello” |
[abc] | Character set (a, b, or c) | [aeiou] matches vowels |
[^abc] | Negated set (not a, b, or c) | [^0-9] matches non-digits |
[a-z] | Character range | [A-Za-z] matches letters |
Quantifiers
| Pattern | Description | Example |
|---|
* | 0 or more | \d* matches “”, “123” |
+ | 1 or more | \d+ matches “1”, “123” |
? | 0 or 1 (optional) | colou?r matches “color”, “colour” |
{n} | Exactly n times | \d{4} matches “2024” |
{n,} | n or more times | \d{2,} matches “12”, “123” |
{n,m} | Between n and m times | \d{1,3} matches “1”, “12”, “123” |
*? | Lazy (minimal match) | ".+?" matches "a" in "a" "b" |
Anchors
| Pattern | Description |
|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Non-word boundary |
Groups and Alternation
| Pattern | Description | Example |
|---|
(abc) | Capturing group | (ha)+ matches “haha” |
(?:abc) | Non-capturing group | (?:ha)+ same but no capture |
\1 | Backreference | (a)\1 matches “aa” |
a|b | Alternation (or) | cat|dog matches “cat” or “dog” |
Lookahead and Lookbehind
| Pattern | Description |
|---|
(?=abc) | Positive lookahead |
(?!abc) | Negative lookahead |
(?<=abc) | Positive lookbehind |
(?<!abc) | Negative lookbehind |
Common Patterns
Email: ^[\w.-]+@[\w.-]+\.\w{2,}$
URL: https?:\/\/[\w.-]+(?:\/\S*)?
IPv4: \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Phone: \+?\d[\d\s-]{7,}\d
Date: \d{4}-\d{2}-\d{2}
Hex Color: #[0-9A-Fa-f]{6}\b
JavaScript Regex Flags
| Flag | Description |
|---|
g | Global — find all matches |
i | Case-insensitive |
m | Multiline — ^ and $ match line boundaries |
s | DotAll — . matches newlines |
u | Unicode support |
Try It Live
Test all these patterns in our Regex Tester — with real-time match highlighting and group extraction.