Boneyard Tools

Regular Expression Cheatsheet

A fast reference to 46 regular-expression tokens, grouped into seven sections from anchors to escapes. Type in the search box to filter by token or by plain-English meaning, and every match shows what it does plus a short worked example. Each row has a Copy button so you can drop the token straight into your pattern.

How to use the regex cheatsheet

  1. Type in the search box to filter the sheet as you go.
  2. Search a token such as \d or a plain word such as digit or lookahead.
  3. Watch the count above the table to see how many tokens match.
  4. Read the Meaning column and the Example column for each row.
  5. Click Copy on a row to put that token on your clipboard.

Examples

Search the token \d

\d
2 tokens: \d (any digit, [0-9]) and \D (any non-digit), since search is case-insensitive

Search the word lookahead

lookahead
2 tokens: (?=...) positive lookahead and (?!...) negative lookahead

Search the word digit

digit
3 tokens: \d, \D and \w, because \w is defined as letters, digits or underscore

Frequently asked questions

What does \d mean in regex?

\d matches any single digit and is the same as the character class [0-9]. Its opposite, \D, matches any non-digit character. So \d\d matches a pair of digits like 42.

What is the difference between * and +?

Both repeat the token before them, but * matches zero or more, so it can match nothing at all, while + matches one or more and needs at least one occurrence. That is why ab*c matches ac, but ab+c does not.

What are greedy and lazy quantifiers?

By default * and + are greedy and grab as much as they can. Adding a ? makes them lazy so they match as little as possible, which is why <.*?> stops at the first > and matches only <a> in <a><b>.

What is a lookahead and a lookbehind?

A lookahead like (?=...) asserts a pattern follows the current spot without consuming it, and (?!...) asserts one does not. Lookbehinds (?<=...) and (?<!...) do the same for what comes before, so (?<=\$)\d+ matches 50 in $50.

What do regex flags like g, i, m and s do?

The g flag returns every match rather than just the first, i makes matching case-insensitive, m lets ^ and $ match at line breaks, and s (dotall) lets the dot match newlines too. The sheet also lists u for Unicode and y for sticky matching.

How do I match a literal special character?

Escape it with a backslash. So \. matches a real dot rather than any character, and \\ matches a single backslash, which is handy for Windows paths like C:\\path.

How does the search box match?

It does a case-insensitive substring match against both the token and its meaning, so searching digit also surfaces \w because its meaning mentions digits. Clearing the box brings back the full sheet grouped by category.

Are all the tokens valid in JavaScript regex?

Most are, but a couple are flagged as reference only. \A and \z are start and end of input anchors from other engines like PCRE and are not supported in JavaScript, where you use ^ and $ instead.

How do named groups and backreferences work?

(?<name>...) captures into a name you choose, and \k<name> refers back to it later in the pattern. Numbered backreferences also exist, so (\w)\1 matches a doubled letter such as the oo in book.

Learn more

Related tools