TSToolSphere
Back to all articles
regex

Regex Cheat Sheet: Quick Reference for Pattern Matching

2026-07-215 min read

Try it: free Regex Tester & Matcher

Test and match regular expressions in real-time with group extraction and replace helpers.

Open →

Character classes

Syntax Matches
. Any character except newline
\d A digit (0–9)
\D A non-digit
\w A word character (letter, digit, underscore)
\W A non-word character
\s Whitespace
\S Non-whitespace
[abc] Any one of a, b, or c
[^abc] Any character except a, b, or c
[a-z] Any lowercase letter (range)

Anchors

Syntax Matches
^ Start of string (or line, with the multiline flag)
$ End of string (or line, with the multiline flag)
\b Word boundary
\B Not a word boundary

Quantifiers

Syntax Meaning
* 0 or more
+ 1 or more
? 0 or 1
{n} Exactly n
{n,} n or more
{n,m} Between n and m
*?, +? Lazy versions — match as little as possible

Groups and alternation

Syntax Meaning
(...) Capturing group
(?:...) Non-capturing group
(?<name>...) Named capturing group
| Alternation (OR)
\1, \2 Backreference to group 1, 2, etc.

Lookarounds

Syntax Meaning
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind
(?<!...) Negative lookbehind

Flags (common across most engines)

Flag Effect
i Case-insensitive matching
g Global — find all matches, not just the first
m Multiline — ^/$ match line boundaries, not just string start/end
s Dot matches newline too

FAQ

What's the difference between \d and [0-9]?
Functionally identical in most engines — \d is shorthand for the digit character class; some engines' \d also matches Unicode digits beyond ASCII 0–9, which [0-9] explicitly does not.

Does . match a newline character?
Not by default — enable the s (dotall) flag if you need . to match newlines too.

What does \b actually detect?
A position where a word character (\w) is adjacent to a non-word character (or string start/end) — useful for matching whole words without accidentally matching inside a longer word.

Test any pattern from this reference live against real sample text with the Regex Tester — entirely client-side.

Looking for other tools?

Explore ToolSphere Homepage →