Regex Cheat Sheet
The complete regular expression reference. Every syntax element, explained with examples.
Introduction
Regular expressions are one of the most powerful tools in a developer's toolkit, but their terse syntax can be intimidating. This cheat sheet is designed to be a practical, at-a-glance reference that you can bookmark and revisit whenever you need to construct or decode a regex pattern. Every entry includes the syntax, a plain-English description, a concrete example, and what it matches.
All examples in this guide use JavaScript's regex flavor. Most syntax is shared across languages (Python, Java, Go, Ruby), but a few features -- like lookbehind support and named groups -- vary. We call out the differences where they matter.
Character Classes
Character classes let you match a single character from a defined set. They are the building blocks of almost every regex pattern, letting you express "match any digit" or "match anything except whitespace" in a compact syntax.
| Syntax | Description | Example | Matches |
|---|---|---|---|
| . | Any character except newline | a.c | "abc", "a1c", "a-c" |
| \d | Any digit (0-9) | \d{3} | "123", "456", "789" |
| \D | Any non-digit | \D+ | "abc", "---", " " |
| \w | Word character (a-z, A-Z, 0-9, _) | \w+ | "hello", "var_1", "Test123" |
| \W | Non-word character | \W+ | "--", "!!", " " |
| \s | Whitespace (space, tab, newline) | hello\sworld | "hello world" |
| \S | Non-whitespace | \S+ | "hello", "123", "---" |
| [abc] | Any character in the set | [aeiou] | "a", "e", "i", "o", "u" |
| [^abc] | Any character NOT in the set | [^0-9] | Any non-digit character |
| [a-z] | Character range (lowercase a through z) | [a-zA-Z] | Any letter, upper or lower |
Quantifiers
Quantifiers control how many times the preceding element must occur. They range from "zero or more" to exact counts. By default, quantifiers are greedy -- they match as much as possible. Append ? to make them lazy (match as little as possible).
| Syntax | Description | Example | Matches |
|---|---|---|---|
| * | Zero or more (greedy) | ab*c | "ac", "abc", "abbc", "abbbc" |
| + | One or more (greedy) | ab+c | "abc", "abbc", but NOT "ac" |
| ? | Zero or one (optional) | colou?r | "color" and "colour" |
| {n} | Exactly n times | \d{3} | "123", "456", but NOT "12" |
| {n,} | n or more times | \d{2,} | "12", "123", "1234", but NOT "1" |
| {n,m} | Between n and m times | \d{2,4} | "12", "123", "1234" |
| *? | Zero or more (lazy) | <.*?> | Matches the shortest tag, not the longest |
| +? | One or more (lazy) | \w+? | Matches one character at a time |
Anchors
Anchors don't match characters -- they match positions in the string. They assert that the current position is at a boundary (start of string, end of line, word edge) without consuming any characters. This makes them essential for ensuring your pattern matches in the right context.
| Syntax | Description | Example | Matches |
|---|---|---|---|
| ^ | Start of string (or line with m flag) | ^Hello | "Hello world" but NOT "Say Hello" |
| $ | End of string (or line with m flag) | world$ | "Hello world" but NOT "world peace" |
| \b | Word boundary | \bcat\b | "cat" but NOT "catch" or "scattered" |
| \B | Non-word boundary | \Bcat\B | "scattered" but NOT "cat" |
Groups and Capturing
Groups serve two purposes: they let you apply quantifiers to sub-expressions, and capturing groups let you extract matched segments for later use (in replacements or in code). Non-capturing groups give you the grouping benefit without the capture overhead.
| Syntax | Description | Example | Matches |
|---|---|---|---|
| (abc) | Capturing group | (foo)(bar) | "foobar", captures "foo" and "bar" |
| (?:abc) | Non-capturing group | (?:foo|bar)+ | "foobar", "barfoo", without capturing |
| (?<name>abc) | Named capturing group | (?<year>\d{4}) | "2026", accessible as groups.year |
| \1 | Backreference to group 1 | (go)\s\1 | "go go" but NOT "go run" |
| a|b | Alternation (or) | cat|dog | "cat" or "dog" |
Lookahead and Lookbehind
Lookahead and lookbehind assertions check what comes before or after the current position without including those characters in the match. They are called "zero-width assertions" because they don't consume characters. For a deep dive, see our dedicated guide on lookahead and lookbehind.
| Syntax | Description | Example | Matches |
|---|---|---|---|
| (?=...) | Positive lookahead | \d+(?= USD) | "100" in "100 USD", not "100 EUR" |
| (?!...) | Negative lookahead | \d+(?! USD) | "100" in "100 EUR", not "100 USD" |
| (?<=...) | Positive lookbehind | (?<=\$)\d+ | "50" in "$50", not in "50" |
| (?<!...) | Negative lookbehind | (?<!\$)\d+ | "50" in "50 items", not in "$50" |
Flags
Flags modify how the regex engine interprets the pattern. They are appended after the closing delimiter in literal syntax (/pattern/flags) or passed as a second argument to new RegExp(pattern, flags). Most patterns need at least the g flag to find all matches.
| Flag | Name | Description |
|---|---|---|
| g | Global | Find all matches, not just the first one |
| i | Case-insensitive | Match uppercase and lowercase equivalently |
| m | Multiline | ^ and $ match start/end of each line, not just the string |
| s | DotAll | . matches newline characters as well |
| u | Unicode | Enables full Unicode matching and \p{...} property escapes |
| y | Sticky | Matches only from the lastIndex position (no searching forward) |
Common Escape Sequences
Some characters have special meaning in regex and must be escaped with a backslash to match literally. If a character is a metacharacter (like ., *, +, ?, (, ), [, ], {}, \, ^, $, |), you need \ before it to treat it as a literal character.
# Match a literal dot
\. Matches "."
# Match a literal asterisk
\* Matches "*"
# Match a literal backslash
\\ Matches "\"
# Match a literal dollar sign
\$ Matches "$"
# Match literal square brackets
\[\] Matches "[]"
# Match literal parentheses
\(\) Matches "()"
# Match literal curly braces
\{\} Matches "{}"Putting It All Together
Here are a few practical patterns that combine multiple concepts from this cheat sheet:
# Match an email address (simplified)
[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}
# Match a URL
https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})[\/\w.-]*
# Match a date in YYYY-MM-DD format
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
# Match an IPv4 address
(?:\d{1,3}\.){3}\d{1,3}
# Match a hex color code
#(?:[0-9a-fA-F]{3}){1,2}\bTry these patterns yourself with our Regex Tester -- paste any pattern and test string to see highlighted matches and a plain-English explanation of what each part does.
Further Reading
- MDN Regular Expressions
MDN guide to JavaScript regular expressions with syntax reference.
- regular-expressions.info
Comprehensive regex tutorial and reference covering all major flavors.
- ECMAScript RegExp specification
The official language specification for JavaScript regex behavior.