Saved

Press the star next to a tool's name to save it.

All my tools

Regex Cheat Sheet

This regex cheat sheet lists regular expression syntax as JavaScript uses it: character classes, anchors, quantifiers, groups, lookarounds and flags. Search it, filter by topic or print it.

Restored your last values ·

Your search stays in your browser. Nothing is uploaded.

Commands

–

No commands match that search.

Recent results

This regex cheat sheet follows the MDN cheatsheet for JavaScript regular expressions and puts the syntax in one searchable list. [1] Other languages, such as Python, PCRE and Java, share most of it but differ in details, so check the flavour you use. To try a pattern on real text, use the regex tester linked below.

How to use the regex cheat sheet

  1. Type in the search box to filter by any word in a name, a command or its note.
  2. Choose a topic to see one group instead of the whole list.
  3. Read the command in the right-hand column and its meaning on the left.
  4. Press Print for a clean copy of whatever is shown.

What is in each topic

The regex cheat sheet is split into 5 topics. The table shows how many entries each topic holds and which ones they are, so you can jump straight to the right group with the topic box.

TopicEntriesWhat it covers
Characters and classes8any character; a digit; a word character; whitespace; any of these; range; none of these; tab and line break
Anchors and boundaries8start of input; end of input; word boundary; not a word boundary; lookahead; negative lookahead; lookbehind; negative lookbehind
Quantifiers7zero or more; one or more; zero or one; exactly n; at least n; between n and m; lazy version
Groups and alternation6capturing group; named group; non-capturing group; either or; backreference; named backreference
Flags5case-insensitive; multiline; dot matches line breaks; unicode; global

Characters and classes

A dot matches any character except a line terminator, and with the s flag it matches line terminators too. \d matches a digit and \D a non-digit, \w matches a Basic Latin letter, digit or underscore and \W the rest, and \s matches white space and \S the opposite. [1] Square brackets list the characters allowed, a hyphen makes a range such as [a-c], and a leading ^ negates the class. [1] Remember that \w covers only A to Z, digits and underscore, so it misses accented letters.

Escapes such as \t for tab and \n for line feed stand for control characters, and \u{...} and \p{...} need Unicode-aware mode. [1]

Anchors, lookarounds and groups

The caret matches the start of input and the dollar sign the end; with the m flag they also match at line breaks. \b is a word boundary and \B its opposite. [1] Lookahead x(?=y) matches x only when y follows, x(?!y) when y does not follow, (?<=y)x when y comes before and (?<!y)x when it does not. The lookaround part is not included in the match. [1]

Parentheses make a capturing group, (?<name>x) names it and (?:x) groups without capturing. A vertical bar means either-or, \1 repeats what group 1 matched and \k<name> repeats a named group. [1]

Quantifiers and flags

A star means zero or more, a plus one or more, a question mark zero or one, {n} exactly n times, {n,} at least n and {n,m} between n and m. By default these are greedy; adding a question mark makes them lazy so they match as few characters as possible. For example, /<.*?>/ matches only the first tag in a string such as an opening tag followed by another. [1]

The i flag ignores case, g finds every match, m changes how ^ and $ behave and s lets the dot cross line breaks. [1] The flag list is not fully described on the MDN cheatsheet, so the notes on i and g here are from our own working. Test greedy patterns on long text, because they can match more than you expect.

Who this page helps

Developers use a cheat sheet to look up a command they have not typed for a while, to learn the names of the options and to compare similar commands. Beginners can read down a topic to see what is possible, and experienced users can search for the one flag they forgot.

Tips for getting the most from the regex cheat sheet

  • Build a pattern in small pieces and test each piece on sample text before you combine them.
  • Anchor patterns with start and end markers when you validate a whole value.
  • Prefer lazy quantifiers or a negated class when a greedy pattern matches too much.

Common mistakes

Most problems come from running a command in the wrong place or with the wrong target. Read a command before you press Enter, replace every placeholder with your own value, and try destructive commands on a test copy first. When a command behaves differently from the note, check the version you have installed and the official documentation.

Where the entries come from

Each command was checked against the documentation listed below this article. Where a command is standard but is not shown on the page we read, the article says it is our own working. Options and defaults change between releases, so run the help command for the version you have installed before you rely on a flag.

What this page does not do

It lists the commands most people need and does not try to cover every option. For the complete syntax, error messages and advanced features, open the official documentation linked in the sources, and keep it beside the cheat sheet while you learn.

Printing and checking

Filter by topic before you print if you only need one section, and the printed page will be shorter. The notes are short on purpose, so keep your textbook or the official documentation open for the full detail and for anything that changes between versions.

What next

Use this often? Add Regex Cheat to your Home screen.

Frequently asked questions

What does this regex cheat sheet cover?

Character classes, anchors, word boundaries, lookahead and lookbehind, groups and backreferences, quantifiers and the common flags.

What is the difference between greedy and lazy?

Greedy quantifiers match as much as they can. Adding a question mark makes them lazy, so they stop at the shortest match.

Does it work in every language?

The core syntax is shared, but lookbehind, named groups and flags differ. This sheet follows JavaScript.

What does \w match?

Basic Latin letters, digits and the underscore. It does not match accented letters or other scripts unless you use Unicode properties.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools