Guide
Regex cheatsheet with live tester
A practical reference to the regular expression syntax developers use every day, grouped by category with tiny examples and a live tester to check your patterns.
A regular expression is a compact pattern for matching text. It describes a set of strings rather than one fixed string, which makes it the right tool for validation, search-and-replace, log parsing, and tokenizing. This page is a working reference for the syntax you reach for most. Paste a pattern and some sample text into the tester above to check any rule as you read.
Anchors and boundaries
Anchors match a position, not a character. They consume nothing.
| Token | Matches | Example |
|---|---|---|
^ |
start of string or line | ^Error matches lines starting with Error |
$ |
end of string or line | ;$ matches lines ending in ; |
\b |
a word boundary | \bcat\b matches cat but not category |
\B |
a non-boundary | \Bcat matches cat inside bobcat |
In multiline mode (m flag), ^ and $ match at every line break instead of only the
string ends.
Character classes
A character class matches one character out of a set.
| Token | Matches |
|---|---|
\d / \D |
a digit / a non-digit |
\w / \W |
a word char ([A-Za-z0-9_]) / a non-word char |
\s / \S |
whitespace / non-whitespace |
. |
any character except newline |
[abc] |
a, b, or c |
[a-z] |
any lowercase letter |
[^0-9] |
any character that is not a digit |
Inside [...] most metacharacters lose their special meaning, so [.+] matches a literal dot or
plus. To include a hyphen literally, put it first or last: [-a-z].
Quantifiers
Quantifiers say how many times the preceding token may repeat.
| Token | Meaning |
|---|---|
* |
zero or more |
+ |
one or more |
? |
zero or one (optional) |
{n} |
exactly n |
{n,} |
n or more |
{n,m} |
between n and m |
By default quantifiers are greedy and grab as much as they can. Append ? to make them
lazy:
<.*> on "<a><b>" → matches "<a><b>"
<.*?> on "<a><b>" → matches "<a>"
Groups and alternation
(...)is a capturing group — it remembers what it matched for back-references and replace.(?:...)is a non-capturing group — it groups without capturing, which is faster and keeps group numbers clean.(?<name>...)is a named group, referenced later asname.|is alternation, meaning "or":cat|dogmatches either word.
(\d{4})-(\d{2})-(\d{2}) captures year, month, day from 2026-06-30
(?:https?|ftp):// matches the scheme without capturing it
Lookaround
Lookaround asserts what is or is not adjacent without consuming it.
| Token | Meaning |
|---|---|
(?=...) |
positive lookahead |
(?!...) |
negative lookahead |
(?<=...) |
positive lookbehind |
(?<!...) |
negative lookbehind |
\d+(?= USD) matches the number in "50 USD" but not the " USD"
(?<=\$)\d+ matches the number in "$50" but not the "$"
Flavor differences: PCRE vs JavaScript
The tokens above behave the same across most engines, but a few details diverge:
- Lookbehind. PCRE has supported it for years. JavaScript only added it in modern engines, so
older runtimes will throw on
(?<=...). - Named groups. PCRE accepts
(?P<name>...)and(?<name>...); JavaScript uses(?<name>...). - The
\A,\Z, and\zanchors exist in PCRE for absolute string boundaries but are absent in JavaScript, where you use^and$without themflag. - Unicode. In JavaScript, properties like
\p{L}require theuflag; PCRE enables them with theumodifier as well, but defaults differ.
When in doubt, test in the same flavor you will deploy to. The tester above runs the JavaScript engine, which covers the vast majority of front-end and Node patterns. Keep patterns readable: prefer named groups over deep numeric back-references, and break long alternations into a verbose pattern where your language supports it.