The Ultimate Regex Cheat Sheet for Developers
Regular expressions are the universal tool for pattern matching in text. This cheat sheet covers character classes, quantifiers, anchors, groups, lookaround assertions, flags, and the most common real-world patterns. Keep it open as a reference while you build and debug your regex.
Character Classes
Character classes define a set of characters to match against a single position in the input string.
| Pattern | Description | Example Match |
|---|---|---|
. | Any character except newline | a, 1, @ |
\d | Any digit [0-9] | 7 |
\D | Any non-digit [^0-9] | a, # |
\w | Any word character [A-Za-z0-9_] | x, 3, _ |
\W | Any non-word character | @, , - |
\s | Any whitespace (space, tab, newline) | , \t, \n |
\S | Any non-whitespace character | a, 5 |
[abc] | Any one of a, b, or c | b |
[^abc] | Any character NOT a, b, or c | d, 1 |
[a-z] | Any lowercase letter | m |
[A-Z] | Any uppercase letter | M |
[0-9] | Any digit (same as \d) | 4 |
[a-zA-Z] | Any letter | Q, q |
For the full specification of character classes and escape sequences, see the MDN Regular Expressions guide.
Quantifiers
Quantifiers specify how many times the preceding element must occur. By default, they are greedy (match as much as possible). Appending ? makes them lazy (match as little as possible).
| Pattern | Type | Description | Example | Matches |
|---|---|---|---|---|
* | Greedy | Zero or more | ab*c | ac, abc, abbc |
+ | Greedy | One or more | ab+c | abc, abbc |
? | Greedy | Zero or one (optional) | colou?r | color, colour |
{3} | Exact | Exactly 3 | \d{3} | 123 |
{2,5} | Greedy | Between 2 and 5 | \w{2,5} | ab, abcde |
{2,} | Greedy | 2 or more | \d{2,} | 12, 123456 |
*? | Lazy | Zero or more (shortest) | ".*?" | First quoted segment |
+? | Lazy | One or more (shortest) | ".+?" | First quoted segment |
{2,5}? | Lazy | Between 2 and 5 (shortest) | \w{2,5}? | ab |
When to use lazy quantifiers: The distinction matters most with .* inside delimiters. The pattern ".*" matches from the first quote to the last quote in the string, while ".*?" matches from the first quote to the nearest closing quote.
Anchors
Anchors match positions in the string, not characters. They do not consume any input.
| Pattern | Description | Example | Matches |
|---|---|---|---|
^ | Start of string (or line with m flag) | ^\d+ | 123 at start of string |
$ | End of string (or line with m flag) | \d+$ | 456 at end of string |
\b | Word boundary | \bcat\b | "cat" but not "concatenate" |
\B | Non-word boundary | \Bcat\B | "cat" inside "education" |
Combining anchors is a common pattern. For example, ^\d+$ ensures the entire string consists of nothing but digits.
Groups and Backreferences
Groups let you treat multiple characters as a single unit, extract submatches, and refer back to captured text.
| Pattern | Description | Example |
|---|---|---|
(abc) | Capturing group, matches and stores "abc" | (foo)bar captures "foo" |
(?:abc) | Non-capturing group, groups without storing | (?:foo)bar matches but does not capture |
(?<name>abc) | Named capturing group | (?<year>\d{4}) captures as "year" |
\1 | Backreference to group 1 | (\w+)\s+\1 matches "the the" |
\k<name> | Backreference to named group | \k<year> references the "year" group |
(a|b) | Alternation, matches "a" or "b" | (cat|dog)s matches "cats" or "dogs" |
Backreference example: The pattern (\w+)\s+\1 matches repeated words like "the the" or "is is". The \1 refers back to whatever the first group captured.
Alternation tip: Use a group to scope the pipe operator. Without it, cat|dogs matches either "cat" or "dogs" (the pipe applies to the entire expression on each side).
Lookahead and Lookbehind
Lookaround assertions match a position based on what comes before or after it, without including that text in the match result. They are zero-width, meaning they do not consume characters.
| Pattern | Name | Description | Example |
|---|---|---|---|
(?=abc) | Positive lookahead | Position is followed by "abc" | \d+(?= dollars) matches 100 in "100 dollars" |
(?!abc) | Negative lookahead | Position is NOT followed by "abc" | \d+(?! dollars) skips "100 dollars" |
(?<=abc) | Positive lookbehind | Position is preceded by "abc" | (?<=\$)\d+ matches 50 in "$50" |
(?<!abc) | Negative lookbehind | Position is NOT preceded by "abc" | (?<!\$)\d+ matches 50 in "EUR50" |
Practical example: To match a number only when it appears after a dollar sign, use (?<=\$)\d+(\.\d{2})?. The dollar sign is required for the match to succeed, but it is not included in the matched text.
Flags
Flags modify how the regex engine processes the pattern. Place them after the closing delimiter in most languages (for example, /pattern/gi).
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | A and a are treated as the same character |
m | Multiline | ^ and $ match start and end of each line |
s | Dotall (single-line) | . matches newline characters too |
u | Unicode | Enable full Unicode matching |
x | Extended | Ignore whitespace and allow inline comments (not all engines) |
Combine flags as needed: /pattern/gim applies global, case-insensitive, and multiline simultaneously.
Common Patterns
A reference table of regex patterns for everyday validation and extraction tasks.
| Use Case | Pattern | Notes |
|---|---|---|
| Email (basic) | ^[\w.-]+@[\w.-]+\.\w{2,}$ | Covers most addresses. For strict RFC 5322 compliance, use a library. |
| URL (http/https) | ^https?:\/\/[\w.-]+\.[a-z]{2,}(\/\S*)?$ | Matches standard web URLs. |
| IPv4 address | ^(\d{1,3}\.){3}\d{1,3}$ | Does not validate octet range (0-255). Add per-octet checks for strict validation. |
| Date (YYYY-MM-DD) | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ | Validates format and month/day ranges. Does not check for leap years. |
| Phone (US) | ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ | Handles formats like (555) 123-4567, 555.123.4567, and 5551234567. |
| Hex color | ^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$ | Matches both shorthand (#FFF) and full (#FFFFFF) hex colors. |
| Username | ^[a-zA-Z0-9_]{3,20}$ | Alphanumeric and underscores, 3 to 20 characters. |
| Strong password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$ | Requires lowercase, uppercase, digit, and special character. Minimum 8 characters. |
| HTML tag | <([a-z]+)([^>]*)>(.*?)<\/\1> | Simple tag matching. For real HTML parsing, use a proper DOM parser. |
| Trim whitespace | ^\s+|\s+$ | Matches leading and trailing whitespace for removal. |
These patterns cover the most frequent use cases. For production validation of emails and URLs in particular, consider using a dedicated library since the edge cases in those specs go well beyond what a single regex can handle.
Frequently Asked Questions
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (*, +, {n,m}) match as much text as possible while still allowing the overall pattern to succeed. Lazy quantifiers (*?, +?, {n,m}?) match as little text as possible. For example, given the input "foo" and "bar", the greedy pattern ".*" matches the entire string from the first quote to the last, while the lazy pattern ".*?" matches "foo" and "bar" separately.
How do I match a literal dot, bracket, or other special character?
Escape the character with a backslash. For example, \. matches a literal period, \[ matches a literal opening bracket, and \\ matches a literal backslash. Inside a character class like [.], most special characters (except \, ], ^, and -) lose their special meaning and do not need escaping.
Are regular expressions the same across all programming languages?
The core syntax (character classes, quantifiers, anchors, and basic groups) is consistent across most languages. However, advanced features differ. Lookbehind support, named groups, Unicode property escapes, and flags vary between JavaScript, Python, Java, Go, and others. Always check your target language documentation for feature availability. The MDN Regular Expressions reference is an excellent starting point for JavaScript.
When should I avoid using regex?
Regular expressions are not well suited for parsing nested or recursive structures like HTML, XML, or JSON. They also become difficult to maintain when the pattern grows beyond a few dozen characters. For structured data formats, use a proper parser. For complex validation logic, break the problem into smaller regex checks or use a validation library. If you are working with JSON specifically, our JSON Formatter is a more reliable approach than regex for validation.
How can I make a complex regex easier to read?
Use the x (extended) flag if your regex engine supports it. This flag allows you to add whitespace and inline comments to your pattern. You can also break long patterns into smaller named groups, assign them to variables, and compose them together in your code. If you are new to regex, our Regex Tutorial for Beginners walks through building patterns step by step.
Test Your Patterns
The best way to learn regex is to experiment. The NebulaTool Regex Tester lets you type a pattern, paste your test string, and see matches highlighted instantly, with a breakdown of what each part of your regex does. It runs entirely in your browser, so your data stays private.
Ready to try it yourself?
Open Regex Tester