Skip to content
DevToolKit

Interactive Regex Cheatsheet

Searchable regex token reference with a live sandbox for testing patterns, match highlighting, capture groups, and substitution.

Search Reference

49 of 49 tokens
.
Any character

Matches any single character except newline (unless s flag is set).

\d
Digit

Matches any digit character (0-9). Equivalent to [0-9].

\D
Non-digit

Matches any character that is not a digit. Equivalent to [^0-9].

\w
Word character

Matches any alphanumeric character plus underscore. Equivalent to [a-zA-Z0-9_].

\W
Non-word character

Matches any character that is not a word character. Equivalent to [^a-zA-Z0-9_].

\s
Whitespace

Matches any whitespace character: space, tab, newline, carriage return, form feed.

\S
Non-whitespace

Matches any character that is not whitespace.

[abc]
Character set

Matches any one of the characters inside the brackets.

[^abc]
Negated set

Matches any character NOT inside the brackets.

[a-z]
Character range

Matches any character in the specified range (inclusive).

[a-zA-Z]
Combined range

Matches any uppercase or lowercase letter using combined ranges.

[0-9a-fA-F]
Hex digit range

Matches hexadecimal characters by combining digit and letter ranges.

^
Start of string

Matches the start of a string (or line in multiline mode).

$
End of string

Matches the end of a string (or line in multiline mode).

\b
Word boundary

Matches the position between a word character and a non-word character.

\B
Non-word boundary

Matches any position that is NOT a word boundary.

*
Zero or more

Matches the preceding element zero or more times (greedy).

+
One or more

Matches the preceding element one or more times (greedy).

?
Zero or one

Matches the preceding element zero or one time (optional).

{n}
Exactly n

Matches the preceding element exactly n times.

{n,}
n or more

Matches the preceding element at least n times (greedy).

{n,m}
Between n and m

Matches the preceding element between n and m times (inclusive, greedy).

*?
Lazy zero or more

Matches zero or more times, but as few as possible (lazy/non-greedy).

+?
Lazy one or more

Matches one or more times, but as few as possible (lazy/non-greedy).

??
Lazy zero or one

Matches zero or one time, preferring zero (lazy/non-greedy).

(abc)
Capturing group

Groups multiple tokens together and captures the matched text for back-referencing.

(?:abc)
Non-capturing group

Groups tokens together without capturing. Useful for applying quantifiers to groups.

(?<name>abc)
Named group

Captures the matched text and assigns it a name for easier reference.

\1
Backreference

Matches the same text as previously captured by the nth capturing group.

(a|b)
Group alternation

Captures one alternative from inside a group. Combines grouping with alternation.

|
Alternation (OR)

Matches the expression before or after the pipe. Acts as a boolean OR.

\\
Escaped backslash

Matches a literal backslash character.

\n
Newline

Matches a line feed character (LF, U+000A).

\t
Tab

Matches a horizontal tab character (U+0009).

\r
Carriage return

Matches a carriage return character (CR, U+000D).

\.
Escaped dot

Matches a literal period/dot character.

\^
Escaped caret

Matches a literal caret character.

\$
Escaped dollar

Matches a literal dollar sign character.

\(
Escaped parenthesis

Matches a literal opening parenthesis.

g
Global

Find all matches rather than stopping after the first match.

i
Case-insensitive

Makes the entire pattern case-insensitive.

m
Multiline

Makes ^ and $ match the start/end of each line rather than the whole string.

s
Dotall (single-line)

Makes the dot (.) match newline characters as well.

u
Unicode

Enables full Unicode matching. Required for proper emoji and surrogate pair handling.

y
Sticky

Matches only at the index indicated by the lastIndex property. Does not search forward.

(?=abc)
Positive lookahead

Asserts that what follows the current position matches the pattern, without consuming characters.

(?!abc)
Negative lookahead

Asserts that what follows the current position does NOT match the pattern.

(?<=abc)
Positive lookbehind

Asserts that what precedes the current position matches the pattern, without consuming characters.

(?<!abc)
Negative lookbehind

Asserts that what precedes the current position does NOT match the pattern.

Live Sandbox

//g
Flags
No matches found

Common Patterns

Was this tool helpful?

How to Use

This interactive regex cheatsheet combines a comprehensive token reference with a live testing sandbox. Browse regex syntax by category, search for specific tokens, and test patterns in real time with instant match highlighting and capture group extraction.

  1. Browse the reference: The left panel organizes 45+ regex tokens into eight categories: Character Classes, Anchors, Quantifiers, Groups, Alternation, Assertions, Escapes, and Flags. Expand or collapse any category to focus on what you need.
  2. Search tokens: Type a keyword (e.g., "lookahead", "digit", or "\d") in the search bar to instantly filter the reference. Results update as you type.
  3. Try It in the sandbox: Hover over any token entry and click "Try It" to load its example pattern and test string into the live sandbox. The pattern executes immediately and shows highlighted matches.
  4. Test your own patterns: Enter any regex pattern in the sandbox input field. Type or paste your test string below. Matches highlight in real time with color-coded spans. Each match shows its index position and any capture groups.
  5. Toggle flags: Click the flag buttons (g, i, m, s, u, y) to enable or disable regex flags. Active flags appear highlighted. The global flag (g) is enabled by default to show all matches.
  6. Use Replace mode: Switch to the Replace tab to test substitution patterns. Enter a replacement string using $1, $2 for back-references or $$<name> for named groups. The result updates live.
  7. Try common patterns: Click any of the pre-built patterns (Email, URL, IPv4, Hex Color, Phone, Date) in the Quick Patterns section to instantly load and test them.

About This Tool

Regular Expression Syntax Overview

Regular expressions (regex) are patterns used to match character combinations in strings. They are supported in virtually every programming language, text editor, and command-line tool. A regex pattern consists of literal characters and metacharacters that define match rules. For example, \d3-\d4 matches a phone number like "555-1234" by specifying exactly three digits, a literal hyphen, and four more digits.

Character Classes and Shorthand Notation

Character classes define sets of characters to match. Square brackets [abc] match any single character from the set. Ranges like [a-z] match any lowercase letter. Negated sets [^0-9] match anything not in the set. JavaScript provides shorthand classes: \d for digits (equivalent to [0-9]), \w for word characters ([a-zA-Z0-9_]), and \s for whitespace. Their uppercase counterparts \D, \W, \S match the inverse.

Quantifiers: Greedy vs Lazy Matching

Quantifiers control how many times the preceding element must match. The asterisk * matches zero or more times, + matches one or more, and ? matches zero or one. Curly braces specify exact counts: {3} for exactly three, {2,5} for two to five. By default, quantifiers are greedy, consuming as many characters as possible. Adding ? after a quantifier makes it lazy (non-greedy), matching the minimum number of characters. This distinction is critical when parsing structured formats like HTML tags, where greedy matching captures too much text.

Groups, Captures, and Back-References

Parentheses () create capturing groups that save matched text for later reference. Back-references like \1 match the same text captured by the first group. Named groups (?<name>...) improve readability by assigning descriptive names to captures. Non-capturing groups (?:...) group tokens without saving the match, which is useful when you need grouping for quantifiers but do not need to extract the matched text. In the Find and Replace tool, capture groups enable powerful text transformations using $1, $2 substitution patterns.

Lookahead and Lookbehind Assertions

Zero-width assertions check for patterns without consuming characters. Positive lookahead (?=...) asserts that what follows matches a pattern. Negative lookahead (?!...) asserts the opposite. Lookbehind assertions (?<=...) and (?<!...) check what precedes the current position. These are invaluable for extracting values that appear in specific contexts, such as matching a number only when preceded by a dollar sign, without including the dollar sign in the match result.

Why Use This Tool

Interactive Learning, Not Just Reference

Static cheatsheets list tokens but do not let you experiment. This tool combines a searchable reference with a live sandbox where every token has a "Try It" button that loads a working example. You see the pattern match immediately, including capture group values and match indices. This makes it significantly faster to learn regex by doing rather than just reading syntax tables.

Built for Developers Who Work Across Languages

Regex syntax is nearly universal across JavaScript, Python, Java, Go, Ruby, and PHP. The core tokens, quantifiers, character classes, and assertions documented here apply in all these languages. Whether you are writing validation logic in a JSON Formatter, building patterns for a Code Diff tool, or extracting data with a Text Extractor, the regex fundamentals remain the same. Everything runs 100% client-side in your browser — your patterns and test data never leave your device.

Protection Against Catastrophic Backtracking

Poorly written regex patterns can cause catastrophic backtracking, where the engine explores exponentially many paths before failing. This sandbox includes a 2-second timeout guard that terminates runaway patterns and warns you about potential ReDoS issues. This safety net lets you experiment freely without freezing your browser tab, making it ideal for testing untrusted or complex patterns before deploying them in production code.

FAQ

What is the difference between greedy and lazy quantifiers in regex?
Greedy quantifiers (*, +, {n,m}) match as many characters as possible, then backtrack if needed. Lazy quantifiers (*?, +?, {n,m}?) match as few characters as possible, expanding only when necessary. For example, given '<b>bold</b>', the greedy pattern '<.*>' matches '<b>bold</b>' (the entire string), while the lazy '<.*?>' matches just '<b>' (the first tag).
How do lookahead and lookbehind assertions work?
Lookahead (?=...) and lookbehind (?<=...) are zero-width assertions that check for a pattern without consuming characters. Positive lookahead (?=abc) asserts that 'abc' follows the current position. Negative lookahead (?!abc) asserts 'abc' does NOT follow. Lookbehind works the same way but checks what precedes the current position. They are essential for matching text in a specific context without including the context in the match.
What does the 'g' flag do and when should I use it?
The global (g) flag tells the regex engine to find all matches in the input string, not just the first one. Without 'g', regex.exec() and String.match() return only the first match. With 'g', String.match() returns an array of all matches, and regex.exec() can be called repeatedly to iterate through matches. Use 'g' whenever you need to find or replace all occurrences.
How do I match special characters literally in regex?
Special regex characters (. * + ? ^ $ { } [ ] ( ) | \) must be escaped with a backslash to match literally. For example, \. matches a period, \( matches an opening parenthesis, and \\ matches a backslash. Inside a character class [.], most special characters lose their meaning and do not need escaping — the dot inside brackets matches a literal dot.