Home/ Glossary/ Regex Cheatsheet
Tools

Regex Cheatsheet

The regex cheatsheet is a comprehensive quick reference for regular expression syntax, covering anchors, character classes, quantifiers, groups, backreferences, lookaheads, lookbehinds, and flags. Each entry includes a brief explanation and a usage example. It serves as a memory aid for developers who know regex but cannot recall a specific syntax detail without opening the documentation.

What is the Regex Cheatsheet?

A regular expression cheatsheet is a structured reference document that lists all major regex syntax elements with short explanations and examples. It covers the PCRE and JavaScript regex flavors that are most commonly used in web development. The cheatsheet is organized by category: anchors (^ $), character classes ([a-z] \d \w), quantifiers (* + ? {n,m}), groups and references ((x) (?:x) \1), lookaheads and lookbehinds ((?=) (?!)), alternation (|), and flags (g i m s u). It is designed for quick lookup, not as a tutorial.

How does it work?

Browse the cheatsheet by scrolling through categorized sections, or use the search field to filter entries by name or token. Each entry shows the regex token, its name, a one-sentence description, and a concrete example with a sample match. Clicking on a token copies it to the clipboard so you can paste it into your regex pattern immediately. The cheatsheet is complemented by the regex tester tool, where you can apply patterns against real text.

Typical Use Cases

  • Looking up the syntax for a named capture group while writing a parser
  • Remembering the correct quantifier for 'one or more' vs 'zero or more'
  • Finding the right lookahead syntax for a password validation pattern
  • Checking which flags are available in JavaScript's RegExp

Step-by-step Guide

  1. Step 1: Identify the type of regex construct you need (quantifier, anchor, group, etc.).
  2. Step 2: Navigate to the corresponding section of the cheatsheet.
  3. Step 3: Read the description and example for the relevant entry.
  4. Step 4: Copy the token and use it in your regex pattern, then test it in the regex tester.

Tips & Notes

  • Use non-capturing groups (?:...) instead of capturing groups (...) when you do not need a backreference — it is faster.
  • The \b anchor matches a word boundary and is safer than ^ and $ for finding words within longer strings.
  • In JavaScript, use the u flag for full Unicode support, especially when matching characters outside the Basic Multilingual Plane.

Frequently Asked Questions

What is the difference between greedy and lazy quantifiers?
Greedy quantifiers (* + {n,}) match as much text as possible. Lazy quantifiers (*? +? {n,}?) match as little as possible. For example, .* in 'abc' would match everything, while .*? would match nothing and expand only as needed.
Are regex flavors the same across languages?
No. JavaScript, Python (re module), PCRE (PHP, Perl), and .NET each have slightly different syntax and features. Most common tokens are shared, but advanced features like possessive quantifiers or atomic groups may differ.
Regex Cheatsheet
Overview of all JavaScript regex syntax elements: character classes, anchors, quantifiers, groups, flags, methods, and common patterns.
Open Tool