Guides Pricing

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.

Try it — Regex Tester
R
/ /
MATCHES
Test text
⇅ drop a text file to load it
Matches

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 as name.
  • | is alternation, meaning "or": cat|dog matches 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 \z anchors exist in PCRE for absolute string boundaries but are absent in JavaScript, where you use ^ and $ without the m flag.
  • Unicode. In JavaScript, properties like \p{L} require the u flag; PCRE enables them with the u modifier 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.

Frequently asked questions

A greedy quantifier like .* matches as much as possible, then backtracks. A lazy quantifier like .*? matches as little as possible. Use lazy quantifiers when a pattern should stop at the first closing delimiter rather than the last.
Escape it with a backslash. A bare dot . matches any character, but \. matches a literal period. The same applies to other metacharacters like * + ? ( ) [ ] { } ^ $ | and the backslash itself.
Outside a character class, ^ anchors to the start of the string (or line, in multiline mode). Inside a character class like [^abc] it negates the class, meaning "any character except a, b, or c".
The common tokens are identical, but advanced features differ. PCRE supports lookbehind, recursion, and named groups with (?P<name>...); older JavaScript engines lack lookbehind and use (?<name>...). Test your pattern in the flavor you will run it in.
Add the i flag. In JavaScript that is /pattern/i; in PCRE it is the i modifier after the closing delimiter. The tester above lets you toggle flags so you can see the effect immediately.

Related

Esc
↑↓ navigate open Esc close