The team behind OnlineTools4Free — building free, private browser tools.
Published Feb 28, 2026 · 8 min read · Reviewed by OnlineTools4Free
Regex Cheat Sheet: The Ultimate Regular Expression Guide
What is Regex and Why Learn It?
A regular expression (regex) is a pattern that describes a set of strings. It is a compact, powerful language for text search, validation, extraction, and replacement. Every programming language supports regex, and it appears in tools from text editors to databases to command-line utilities.
Regex has a reputation for being cryptic, but the core concepts are straightforward. A pattern like \d{3}-\d{4} matches phone numbers in the format "555-1234" — three digits, a hyphen, four digits. Once you learn the building blocks, you can construct patterns for almost any text matching task.
Use our Regex Tester to try patterns as you read this guide — paste in test text, write a pattern, and see matches highlighted in real time.
Basic Syntax: Characters and Metacharacters
Most characters in a regex match themselves literally. The letter a matches "a", the digit 5 matches "5". The power comes from metacharacters — characters with special meanings:
.— Matches any single character except newline.c.tmatches "cat", "cot", "cut".^— Matches the start of a line.^Hellomatches "Hello world" but not "Say Hello".$— Matches the end of a line.end$matches "the end" but not "endless".\— Escapes a metacharacter to match it literally.\.matches an actual period.|— Alternation (OR).cat|dogmatches "cat" or "dog".
Character classes
[abc]— Matches any one character in the set: a, b, or c.[a-z]— Matches any lowercase letter.[A-Za-z0-9]— Matches any letter or digit.[^abc]— Matches any character NOT in the set.
Shorthand character classes
\d— Any digit. Equivalent to[0-9].\w— Any word character. Equivalent to[A-Za-z0-9_].\s— Any whitespace (space, tab, newline).\D,\W,\S— Negated versions (non-digit, non-word, non-whitespace).
Quantifiers: How Many Times to Match
Quantifiers specify how many times the preceding element should be matched:
*— Zero or more times.ab*cmatches "ac", "abc", "abbc", "abbbc".+— One or more times.ab+cmatches "abc", "abbc" but not "ac".?— Zero or one time (optional).colou?rmatches "color" and "colour".{n}— Exactly n times.\d{4}matches exactly four digits.{n,}— At least n times.\d{2,}matches two or more digits.{n,m}— Between n and m times.\d{2,4}matches 2, 3, or 4 digits.
Greedy vs lazy: By default, quantifiers are greedy — they match as much as possible. Adding ? after a quantifier makes it lazy (matches as little as possible). For example, given the text <b>bold</b>:
<.*>(greedy) matches the entire string<b>bold</b>.<.*?>(lazy) matches<b>first, then</b>.
Groups and Capturing
Parentheses create groups that serve two purposes: grouping for quantifiers and capturing matched text for extraction.
(abc)— Capturing group. Matches "abc" and captures it for back-references or extraction.(?:abc)— Non-capturing group. Groups without capturing. Use when you need grouping but not the captured text.(\d{3})-(\d{4})— Two capturing groups. For input "555-1234", group 1 captures "555" and group 2 captures "1234".\1,\2— Back-references to captured groups.(\w+) \1matches repeated words like "the the".
Named groups
Named groups make regex more readable: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) captures date parts with names instead of numbers. Access them as match.groups.year in JavaScript.
Lookaheads and Lookbehinds
Lookarounds assert that something exists (or does not exist) before or after the current position, without including it in the match:
(?=...)— Positive lookahead.\d+(?=px)matches "12" in "12px" but not in "12em".(?!...)— Negative lookahead.\d+(?!px)matches "12" in "12em" but not in "12px".(?<=...)— Positive lookbehind.(?<=\$)\d+matches "50" in "$50" but not in "50".(?<!...)— Negative lookbehind.(?<!\$)\d+matches "50" when NOT preceded by "$".
Lookarounds are zero-width — they check a condition but do not consume characters. This is useful when you need to match text based on context without including the context in the result.
Common Real-World Patterns
Here are tested patterns for frequent validation tasks:
Email (simplified)
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Covers the vast majority of real email addresses. Note: fully RFC-compliant email validation via regex is extremely complex and rarely necessary.
URL
https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-._~:?#\[\]@!$&'()*+,;=]*
Phone number (US format)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches "(555) 123-4567", "555-123-4567", "555.123.4567", and "5551234567".
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
IP address (IPv4)
\b(?:\d{1,3}\.){3}\d{1,3}\b
Matches the format but does not validate that each octet is 0-255. For strict validation, use a more complex pattern or validate programmatically after matching.
HTML tags
<([a-z]+)[^>]*>(.*?)<\/\1>
Matches simple opening and closing tag pairs. Important: regex is not the right tool for parsing HTML in production — use a proper HTML parser. But for quick extraction tasks in known-structure content, it works.
Test all these patterns live in our Regex Tester. Paste your test data, try the pattern, and refine until it matches exactly what you need.
Regex Tester
Test and debug regular expressions with real-time matching and explanations.
OnlineTools4Free Team
The OnlineTools4Free Team
We are a small team of developers and designers building free, privacy-first browser tools. Every tool on this platform runs entirely in your browser — your files never leave your device.
