JavaScript RegExp reference

JavaScript Regex Cheat Sheet: Syntax, Flags & Examples

JavaScript regular expressions combine a pattern with flags. Start with literals, character classes, anchors, and quantifiers; add groups or lookarounds only when the match requires them. Test the exact pattern in the same JavaScript runtime because regex flavor and supported flags matter.

At a glance

Quick answer

  • ^ and $ anchor positions, while character classes such as \d, \s, and [A-Z] describe what can match at one position.
  • Quantifiers *, +, ?, and {n,m} repeat the preceding token; adding ? after a quantifier makes that repetition lazy.
  • Parentheses create capture groups unless you use a non-capturing form such as (?:...), and named groups use (?<name>...).
  • Flags change matching behavior: g scans globally, i ignores case, m changes line anchors, s lets dot match line terminators, and u enables Unicode-aware parsing semantics.

Core matching tokens

Most patterns are easier to debug when they begin with simple tokens. Literal characters match themselves unless they have special regex meaning, while a backslash can introduce a shorthand class or escape a metacharacter.

  • . matches a character according to the selected dot and newline behavior.
  • \d matches a decimal digit; \D matches a non-digit.
  • \s matches whitespace; \S matches non-whitespace.
  • \w and \W use JavaScript's word-character rules, which are not a general definition of all Unicode letters.
  • [abc] matches one listed character; [^abc] matches one character not listed.
  • ^ and $ match boundaries rather than consuming ordinary text.

Simple anchored ID

^ID-\d{4}$

Quantifiers control repetition

A quantifier applies to the token or group immediately before it. Greedy quantifiers prefer the longest match that still allows the overall pattern to succeed. A following ? makes the quantifier lazy so it initially prefers fewer repetitions.

  • * means zero or more.
  • + means one or more.
  • ? means zero or one when used as a quantifier.
  • {n} means exactly n; {n,} means at least n; {n,m} means between n and m.
  • *?, +?, ??, and {n,m}? are lazy forms.

Greedy vs lazy

<.*>
<.*?>

Groups organize matches and captures

A capturing group (...) records the substring matched by that group. A non-capturing group (?:...) groups alternatives or quantifiers without adding a numbered capture. Named groups use (?<name>...) and make downstream code easier to read when the capture has a stable meaning.

Alternation uses |. Put alternatives inside a group when they should share surrounding tokens; otherwise alternation precedence can make only part of the expression apply to one branch.

Named date groups

(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

Grouped alternatives

^(?:cat|dog)s?$

Lookarounds assert context without consuming it

A positive lookahead (?=...) requires the following text to match a condition without including that condition in the consumed result. A negative lookahead (?!...) requires the following text not to match the condition.

Lookbehinds are also part of modern JavaScript regex syntax, but support should still be tested in the runtime where a pattern will execute when compatibility with older environments matters.

Require a following unit

\d+(?=px)

Exclude a suffix

foo(?!bar)

Choose flags deliberately

Flags belong to the RegExp, not to an individual group. The g flag finds successive matches, i enables case-insensitive matching, m lets ^ and $ work around line boundaries, s changes dot so it can match line terminators, and u enables Unicode-aware pattern parsing and code-point behavior.

JavaScript also has runtime-specific support for additional standardized flags. If a browser does not support a flag, constructing the RegExp fails before any text is tested, so use the same target runtime when compatibility matters.

Pattern with common flags

/^error:.*$/gim

Reuse captures in replacement text

JavaScript String.replace can reuse the entire match and captured substrings. Numbered captures use forms such as $1 and $2, while named captures can be referenced with $<name>. Global replacement still depends on the pattern carrying the g flag.

Swap two captured words

Pattern: (\w+),\s*(\w+)
Replacement: $2 $1