Orbitautil
Regex Guide

Regex Cheat Sheet

The tokens you use most in JavaScript regular expressions, on one page. Each entry describes how the browser's RegExp behaves; other engines (Python, PCRE) differ in a few details.

Character Classes

  • . Any character except line breaks (the s flag includes them)
  • \w Word character (a-z, A-Z, 0-9, _)
  • \d Digit (0-9)
  • \s Whitespace: space, tab, line breaks and other Unicode spaces
  • \W Not a word character
  • \D Not a digit
  • \S Not whitespace
  • [abc] Any of a, b, or c
  • [^abc] Not a, b, or c
  • [a-g] Character between a & g

Anchors & Boundaries

  • ^ Start of the string (of each line with the m flag)
  • $ End of the string (of each line with the m flag)
  • \b Word boundary
  • \B Non-word boundary

Quantifiers

  • * 0 or more
  • + 1 or more
  • ? 0 or 1 (Optional)
  • {3} Exactly 3
  • {3,} 3 or more
  • {3,5} Between 3 and 5
  • *? +? Lazy: match as little as possible

Groups & Lookarounds

  • (abc) Capture group
  • (?:abc) Non-capturing group
  • (?=abc) Positive lookahead
  • (?!abc) Negative lookahead
  • (?<=abc) Positive lookbehind
  • (?<!abc) Negative lookbehind
  • (?<name>abc) Named capture group
  • \1 Backreference to group 1
  • \k<name> Backreference to a named group
  • a|b a or b

Flags

  • g Global: find all matches, not just the first
  • i Ignore case
  • m Multiline: ^ and $ match at each line
  • s dotAll: . also matches line breaks
  • u Unicode: treat emoji and other astral characters as one character
  • v Unicode sets: like u, plus set operations in classes
  • y Sticky: match only at lastIndex
  • d Indices: report start and end of each group

Frequently asked questions

What is the difference between * and + ?

The asterisk (*) matches 0 or more occurrences, meaning the character is optional. The plus (+) matches 1 or more occurrences, meaning it is required at least once.

How do I match a literal dot or asterisk?

Use a backslash (\) to escape special characters. For example, \. matches a literal dot, and \* matches a literal asterisk.

What are capture groups?

Capture groups, wrapped in parentheses (like `(abc)`), allow you to extract specific parts of a match, which is useful for data extraction and replacement.