chrome-extension://regex-tester-pro · live preview

Pattern

/\b\w+@\w+\.\w+\b/g

Test string

Contact us at sales@example.com for pricing.

flags: g · 1 match · 0.4 ms

All matching happens in your browser. Patterns and test strings never leave the tab.

Regex Guide

Regex Cheatsheet With Examples for JavaScript, Python and PCRE

Updated May 2026 8 min read By the Regex Tester Pro team

Quick answer

A useful cheatsheet should not stop at "\d = digit". This one pairs each token with a tested example, calls out the differences between JavaScript, Python, and PCRE, and tells you when a feature is missing in your runtime. Open Regex Tester Pro alongside it to try every pattern live.

Most regex cheatsheets are reference tables that assume you already know what to do with them. This one is built around examples you can paste into a tester and see working in 10 seconds. Each section links the tokens to a concrete pattern.

JavaScript
// Anchors and character classes in 4 lines
/^\s*$/                                // blank line
/[a-zA-Z][a-zA-Z0-9_]*/                 // identifier-ish
/\b(?:GET|POST|PUT|DELETE)\b/          // HTTP verb
/(?<=#)[a-z0-9-]+/                     // anchor tag, no leading #

Anchors and word boundaries

Anchors do not match characters, they match positions. ^ anchors to the start of the input (or start of a line with the m flag). $ anchors to the end. \b is the boundary between a word character and a non-word character, and is the single most underused token in everyday regex.

JavaScript and PCRE both treat \b as ASCII-only by default. Python's re uses Unicode word characters when the re.UNICODE flag is on (the default in Python 3). If you mix language flavors, this is the most common silent bug.

Character classes you actually use

The shorthand classes are the same across flavors:

Custom classes use brackets, with negation via ^ as the first character: [a-fA-F0-9] matches a hex digit, [^"]+ matches one or more non-quote characters. Escaping inside a class is mostly the same, except ], \, and (sometimes) - need backslashes.

Quantifiers, greedy and lazy

Quantifiers attach to the previous atom: ? (zero or one), * (zero or more), + (one or more), {n,m} (between n and m). They are greedy by default. Adding ? makes them lazy.

Catastrophic backtracking happens when nested greedy quantifiers create exponential possibilities. The classic offender is (a+)+$ against a long string of as ending in b. Always test long inputs against any pattern with nested quantifiers before shipping it.

Groups, alternation, references

Parentheses do two things: they group, and they capture. (cat|dog)s? matches "cat", "cats", "dog", "dogs". The captured text is available as $1 in replacements and as match[1] in JS or m.group(1) in Python.

Non-capturing groups, (?:...), group without saving the text. Use them for alternation or quantification when you do not need the value back. Named groups, (?<name>...), are supported in JS (since ES2018), Python (since forever), and PCRE.

Backreferences inside the pattern, \1 or \k<name>, match the same text the group captured. (\w)\1 matches doubled letters: letter, book, tomorrow.

Lookaround: the assertion family

Lookarounds are zero-width assertions, they look at characters but do not consume them. The four forms:

Lookbehind is fully supported in modern JS engines (V8, SpiderMonkey, JavaScriptCore) and in Python's re (with fixed-width patterns) and regex module (variable width). PCRE supports it natively. Old Safari (pre 16.4) did not, which is why "lookbehind broke my site" was a bug class for years.

Replacement patterns

Replacements are where regex pays its rent. The basics:

The function form is the strongest tool in the kit. The callback receives the full match plus each capture group, lets you compute the replacement procedurally, and works in JavaScript, Python (re.sub with a function), and PCRE through callouts.

Try every pattern from this cheatsheet, live

Regex Tester Pro gives you live matching, named groups, replacements, and reference cheatsheets in one Chrome popup. Free, private, no signup.

Add to Chrome, free

Frequently asked questions

Why does my JavaScript regex behave differently in Python?
Word characters and case folding are Unicode-aware in Python 3 by default but ASCII-only in JavaScript unless you add the u flag. Lookbehind support also differs slightly between engines.
Should I use named capture groups everywhere?
For anything you will read again in three months, yes. The cost is a few extra characters; the benefit is that the regex documents itself.
How do I avoid catastrophic backtracking?
Avoid nested greedy quantifiers (a+)+ patterns, prefer atomic groups or possessive quantifiers in PCRE, and test patterns against pathological long inputs.
What is the difference between greedy and lazy?
Greedy quantifiers grab as much as possible, then back off until the rest of the pattern matches. Lazy quantifiers grab as little as possible, then take more until the rest matches.
How do I match a literal special character?
Escape it with a backslash: \. for a literal period, \? for a literal question mark, \\ for a literal backslash.

More by Peak Productivity

Free developer tools, made for the browser

Privacy-first utilities that run locally. No upload, no signup, no watermark.