By · Updated

Regex Cheat Sheet

Character Classes, Lookarounds, Quantifiers and Flags, Each With an Example

No ratings yet

Loading…

OneTask is free, with no sign-up, and built by one person.
If this tool saved you time, a coffee helps keep new ones coming.

How the Regex Cheat Sheet works

This regex cheat sheet lists the regular expression syntax you reach for every day — character classes, anchors, quantifiers, groups, lookarounds, flags and replacement tokens — with a short example for each. It follows JavaScript, which most modern engines match closely. Search a token, print the page, or test a pattern in the tester linked below.

How to use the regex cheat sheet

Rows are grouped by what a token does. Characters and classes match one character; anchors match a position without consuming anything; quantifiers say how many times the previous item repeats; groups capture or combine items; lookarounds test what comes before or after without including it in the match. Flags change how the whole pattern behaves, and replacement tokens are used in the replace string, not the pattern.

Greedy and lazy quantifiers

Quantifiers are greedy by default: <.+> on <a><b> matches the whole string, because .+ takes as much as it can and only gives back enough to let > match at the end. Adding ? makes the quantifier lazy, so <.+?> stops at the first >. For HTML-like text, a negated class such as <[^>]+> is faster and clearer than either.

Differences between engines

JavaScript, Python, PCRE, Java and .NET agree on almost everything in this table. The differences are at the edges: Python writes named groups as (?P<name>...), POSIX tools such as older grep lack \d and lookarounds, and lookbehind was only added to Safari in 2023. When a pattern must run in several places, test it in each one.

FAQ

What does \b match in a regex?

\b matches a word boundary: the position between a word character and a non-word character, or at the start or end of the string next to a word character. It matches no characters itself, so \bcat\b finds the word cat but not the cat inside category or concatenate.

How do I match a literal dot or bracket?

Put a backslash before it. Characters with special meaning — . * + ? ^ $ { } ( ) [ ] | \ and / in a JavaScript literal — must be escaped to match themselves. Inside a character class most of them lose their meaning, so [.] also matches a literal dot.

Can I print or share this regex cheat sheet?

Yes. Print the page for a one-sheet reference, or download the CSV or JSON and put it in your own documentation. The table is written by OneTask and released under CC0, so there is no licence to follow and no attribution required.

See also: all Tech Data tools, Everyday Data.

Related tools