Regex Cheatsheet

Complete regular expression syntax reference with live match testing and highlighting

A regex cheatsheet (regular expression reference) gives you instant access to the syntax patterns you need without digging through documentation. Use the sections below to look up character classes, quantifiers, anchors, groups, and lookaheads — then test your pattern live in the interactive tester at the top.

Live Regex Tester

/ / gi

Common Presets

Character Classes

PatternDescriptionExample match
Any character except newline a.c → "abc", "aXc"
Word character [a-zA-Z0-9_] \w+ → "hello", "foo_1"
Non-word character \W → " ", "!", "@"
Digit [0-9] \d+ → "42", "2024"
Non-digit \D → "a", " ", "-"
Whitespace (space, tab, newline) \s+ → "   ", "\t"
Non-whitespace \S+ → "hello", "42"
Character set — matches a, b, or c [aeiou] → vowels
Negated set — not a, b, or c [^0-9] → non-digits
Range — lowercase a to z [a-zA-Z] → any letter

Quantifiers

PatternDescriptionExample match
0 or more (greedy) ab* → "a", "ab", "abbb"
1 or more (greedy) ab+ → "ab", "abbb"
0 or 1 — optional colou?r → "color", "colour"
Exactly 3 times \d{3} → "555"
Between 2 and 5 times \w{2,5} → "ab", "hello"
2 or more times \d{2,} → "12", "2024"
0 or more (lazy) a.*?b → shortest "aXb"
1 or more (lazy) a.+?b → shortest match
0 or more (possessive) No backtracking (JS: not supported)

Anchors

PatternDescriptionExample match
Start of string (or line with m flag) ^\d → "4" in "42 apples"
End of string (or line with m flag) \d$ → "9" in "I have 9"
Word boundary \bcat\b → "cat" not "catch"
Non-word boundary \Bcat\B → "cat" in "concatenate"

Groups & Alternation

PatternDescriptionExample match
Capturing group (\d+) captures digits
Non-capturing group (?:ab)+ → "ababab"
Named capturing group (?<year>\d{4})
Back-reference to group 1 (\w+) \1 → "the the"
Alternation — matches a or b cat|dog → "cat", "dog"

Lookahead & Lookbehind

PatternDescriptionExample match
Positive lookahead — followed by \d+(?= px) → "12" in "12 px"
Negative lookahead — not followed by \d+(?! px) → digits without px
Positive lookbehind — preceded by (?<=\$)\d+ → "42" in "$42"
Negative lookbehind — not preceded by (?<!\$)\d+ → "42" not in "$42"

Flags

FlagDescriptionUsage
Global — find all matches /\d+/g
Case-insensitive /hello/i → "Hello", "HELLO"
Multiline — ^ and $ match line edges /^\w+/gm
DotAll — . matches newline too /a.b/s → "a\nb"
Unicode — enables full Unicode support /\p{Emoji}/u
Sticky — match at lastIndex position /\d+/y
Indices — report start/end of matches /\d+/d (ES2022)

Escape Sequences & Special Characters

SequenceDescriptionNotes
Newline Use s flag for . to match \n
Tab Horizontal tab character
Carriage return Windows line endings use \r\n
Literal dot (escaped metachar) Escape: . * + ? ^ $ { } [ ] | ( ) \
Unicode code point \u0041 → "A"
Copied to clipboard!

How to Use the Regex Cheatsheet

Regular expressions (regex) are compact pattern strings that describe how to match, find, or replace text. They are supported in virtually every programming language and text editor. This regex cheatsheet covers the universal syntax that works in JavaScript, Python, PHP, Ruby, Java, and most other environments.

Step 1: Find the syntax you need

Each collapsible section covers a different category of regex syntax. Click any section header to expand or collapse it. The table columns show the pattern, what it does, and a concrete example of what it matches. All sections start open so you can scan everything at a glance.

Step 2: Click a pattern to copy it

Every pattern in the cheatsheet is a clickable button. Clicking one copies the pattern to your clipboard instantly — no text selection required. A toast notification confirms the copy. You can also click any pattern to auto-load it into the live tester above so you can experiment with it immediately.

Step 3: Test your pattern live

The live tester at the top of the page lets you enter any regex pattern and a test string. Matches are highlighted in the output panel in real time. Toggle the g, i, m, s, and u flags using the checkboxes. If your pattern is invalid, an error message is shown below the input.

Step 4: Use the common presets

The presets section provides ready-made regex patterns for the most common use cases: email addresses, URLs, phone numbers, dates, IP addresses, hex color codes, and more. Click any preset to instantly load the pattern into the tester with a sample string so you can see how it works before adapting it to your needs.

Key regex concepts to remember

Greedy vs. lazy: By default, quantifiers like * and + are greedy — they match as much as possible. Add a ? after them (*?, +?) to make them lazy — matching as little as possible.

Anchors are zero-width: ^ and $ don't match characters — they match positions. \b matches the boundary between a word character and a non-word character.

Escape metacharacters: The characters . * + ? ^ $ { } [ ] | ( ) \ have special meaning in regex. To match them literally, escape with a backslash: \. matches a literal dot.

Lookaheads are zero-width too: (?=...) and (?!...) assert what follows the current position without consuming characters, so they don't appear in the match result.

Frequently Asked Questions

Is this regex cheatsheet free to use?

Yes, the regex cheatsheet is 100% free with no signup, no account, and no limits. The live test area also runs entirely in your browser.

Is my test data private?

Absolutely. The live test area runs entirely in your browser using JavaScript. Your regex patterns and test strings are never sent to any server and never stored.

What regex flavor does the live tester use?

The live tester uses JavaScript's built-in RegExp engine. JavaScript regex is similar to PCRE but has some differences — notably, it uses named groups with the (?<name>...) syntax and supports the global, case-insensitive, multiline, dotAll, sticky, and unicode flags.

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (like * and +) match as much as possible while still allowing the overall pattern to succeed. Lazy quantifiers (like *? and +?) match as little as possible. For example, a.*b on 'aXbYb' greedy matches 'aXbYb', while a.*?b matches only 'aXb'.

What is the difference between a capturing group and a non-capturing group?

A capturing group (...) captures the matched text so you can reference it later via $1, $2, etc. or group[1] in code. A non-capturing group (?:...) groups the pattern for quantifiers or alternation without creating a back-reference, which is slightly faster and cleaner when you don't need the capture.

What is the difference between lookahead and lookbehind?

Lookahead (?=...) and negative lookahead (?!...) assert what follows the current position without consuming characters. Lookbehind (?<=...) and negative lookbehind (?<!...) assert what precedes the current position. Both are zero-width — they don't include the matched text in the result.

How do I match a literal dot, star, or parenthesis in regex?

Escape special characters with a backslash. For example, \. matches a literal dot, \* matches a literal asterisk, \( and \) match literal parentheses, and \\ matches a literal backslash. Any metacharacter (.^$*+?{}[]|()\) must be escaped to be treated as a literal character.

Can I use this cheatsheet for Python or other languages?

Most of the syntax shown here applies to PCRE-compatible engines used in Python (re module), PHP, Ruby, Java, and .NET. The main differences are in flag syntax and named group syntax. Python uses (?P<name>...) for named groups, while JavaScript uses (?<name>...).