Regular Expressions Quick Reference: Common Patterns Explained
June 5, 2026 · 4 min read
Regular expressions (regex) are a pattern language for matching, searching, and replacing text. They look intimidating at first — /^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$/ — but most real-world regex consists of a handful of building blocks used repeatedly. This guide covers the patterns you'll reach for most often.
The basics in two minutes
A regex is a sequence of characters that describes a pattern. In JavaScript you write them as /pattern/flags or new RegExp("pattern", "flags").
| Pattern | Matches |
|---|---|
. |
Any character except newline |
\d |
A digit (0–9) |
\w |
A word character (a–z, A–Z, 0–9, _) |
\s |
Whitespace (space, tab, newline) |
[abc] |
Any of: a, b, or c |
[^abc] |
Anything except a, b, or c |
^ |
Start of string / line |
$ |
End of string / line |
+ |
One or more of the previous |
* |
Zero or more of the previous |
? |
Zero or one of the previous |
{n,m} |
Between n and m of the previous |
(…) |
Capture group |
(?:…) |
Non-capturing group |
| `a | b` |
Test any of these in your browser instantly with the RegExp Tester.
Common patterns
Email address (basic)
[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}
This matches the vast majority of real email addresses. RFC 5322 allows much more exotic formats, but this pattern is correct for 99%+ of practical use cases. To extract all emails from a block of text, try the Extract Email Addresses tool.
URL
https?://[\w.-]+(?:/[\w./?=%&#+@-]*)?
Matches http:// and https:// URLs, including paths and query strings. To pull all URLs out of a document, use Extract URLs.
IP address (IPv4)
\b(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b
Matches valid IPv4 addresses (0.0.0.0 – 255.255.255.255). Looks complex, but each group just handles the three ranges: 250–255, 200–249, 0–199.
ISO date (YYYY-MM-DD)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Hex color code
#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
Matches 3-digit (#fff) and 6-digit (#ffffff) hex colour codes.
Integer or decimal number
-?\d+(?:\.\d+)?
Matches integers and decimals, optionally negative.
Slug (URL-safe identifier)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Matches lowercase alphanumeric slugs with single hyphens between words — no leading, trailing, or double hyphens.
Flags
Flags modify how the pattern is applied:
| Flag | Effect |
|---|---|
g |
Global — find all matches, not just the first |
i |
Case-insensitive |
m |
Multiline — ^ and $ match line starts/ends |
s |
Dotall — . matches newlines too |
Example: /hello/gi matches "Hello", "HELLO", "hello" anywhere in the string.
Lookahead and lookbehind
Lookarounds let you match a position without consuming characters:
// Positive lookahead: match "foo" only when followed by "bar"
/foo(?=bar)/
// Negative lookahead: match "foo" only when NOT followed by "bar"
/foo(?!bar)/
// Positive lookbehind: match "bar" only when preceded by "foo"
/(?<=foo)bar/
Lookbehind is supported in modern browsers (Chrome 62+, Firefox 78+, Safari 16.4+).
Find and replace with capture groups
Capture groups (parentheses) let you reference matched text in the replacement string:
// Swap first and last name
"Smith, John".replace(/(\w+), (\w+)/, "$2 $1");
// → "John Smith"
In the replacement, $1 refers to the first capture group, $2 to the second. The Find and Replace Text tool supports regex replacement with the same $1, $2 syntax — useful for batch-reformatting text without writing code.
Common mistakes
Forgetting to escape special characters. The characters ., *, +, ?, (, ), [, ], {, }, ^, $, |, and \ are special in regex. To match a literal period, write \. not ..
Using .+ when you mean [^\n]+. The . pattern doesn't match newlines by default, so if your input spans multiple lines you may need the s (dotall) flag or a character class.
Greedy vs lazy matching. .* is greedy — it matches as much as possible. .*? is lazy — it stops as soon as the rest of the pattern can match. If <.*> matches the entire string <a>foo</a>, try <.*?> to match each tag separately.
Use the RegExp Tester to experiment safely — it shows each match and capture group as you type, so you can tune your pattern without writing a script.