Regex Cheat Sheet

JavaScript regex tokens, flags and tested examples, with correct backslashes, capture groups, word boundaries and constructor escaping.

← Developer Tools

A compact JavaScript-oriented regex reference for common tokens, anchors, groups and quantifiers.

DeveloperRegex ReferenceJavaScript examples

Common regex tokens

PatternMeaning
.Any character except line terminators unless dotAll is enabled
\dASCII digit, equivalent to [0-9]
\wASCII letters, digits and underscore; not a general Unicode word matcher
\sWhitespace
^ / $Start / end of input; with m, start / end of a line
[abc]One character from a set
[^abc]One character not in a set
* / + / ?0+ / 1+ / optional repetitions
{n,m}Repeat from n to m times
(...)Capturing group
(?:...)Non-capturing group
a|bAlternation
\bWord boundary assertion; inside a character class, this escape means backspace

Useful JavaScript flags

g global, i case-insensitive, m multiline, s dotAll, u Unicode mode and y sticky matching.

Flavor matters

Regex syntax differs across JavaScript, PCRE, Python, .NET and other engines. Test patterns in the engine where they will actually run.

Patterns to try

Enter these patterns without surrounding slashes in the Regex Tester. Use the listed flags.

TaskPatternFlagsExample
Find digit runs\d+gIn “A12 B345”, matches 12 and 345.
Find a whole word\bcat\bgIn “cat scatter”, matches cat only.
Match a literal period\.gIn “a.b”, matches the period.
Capture two fields([A-Z]+)-(\d+)gIn “AB-42”, captures AB and 42.

Backslashes in JavaScript strings

A regex literal such as /\d+/g uses one backslash. In a JavaScript string passed to the constructor, escape it again: new RegExp("\\d+", "g"). In this site’s tester, enter \d+ directly. Typing just d+ matches the letter d instead of digits.

Common mistakes

Use \. for a literal period; an unescaped dot is a wildcard. Quantifiers are greedy by default; adding ?, as in .*?, makes them lazy. Group alternatives when applying anchors: ^(cat|dog)$ applies both anchors to either choice.

Word boundaries follow JavaScript’s word-character rules and may not split words as expected in multilingual text. A pattern working on a short example can also be slow on long input; test realistic non-matching cases. For detailed syntax, consult MDN’s regular expression cheat sheet.

Related tools