Back to home

Regex Cheatsheet

Searchable JavaScript regex syntax reference, grouped by category

53 entries
Character classes
.

Any character except a line break

a.c → abc

\d

A digit 0–9

\d+ → 2026

\D

Anything that is not a digit

\w

Letter, digit or underscore

\w+

\W

Anything that is not a word character

\s

Whitespace: space, tab, line break

\S

Anything that is not whitespace

[abc]

One of the characters in the set

[aeiou]

[^abc]

Any character not in the set

[a-z]

A range of characters

[A-Z0-9]

\p{L}

A Unicode property (needs the u flag)

\p{Script=Han}

\P{L}

Anything outside that Unicode property

\P{L}+

Quantifiers
*

The previous item zero or more times

+

The previous item one or more times

?

The previous item zero or one time

{n}

Exactly n times

\d{4}

{n,}

At least n times

{n,m}

Between n and m times

*?

Add ? to any quantifier to make it lazy, matching as little as possible

<.+?>

Anchors
^

Start of input (or of a line with the m flag)

$

End of input (or of a line with the m flag)

\b

A word boundary

\bcat\b

\B

Not a word boundary

Groups and backreferences
(abc)

Capturing group, usable as $1 or \1

(\d{4})-(\d{2})

(?:abc)

Non-capturing group: groups without storing

(?<name>abc)

Named capturing group

(?<year>\d{4})

a|b

Alternation: match either side

cat|dog

\1 \2

Backreference to an earlier group

(\w)\1

\k<name>

Backreference to a named group

Lookaround
(?=abc)

Positive lookahead: what follows must match

\d+(?= 元)

(?!abc)

Negative lookahead: what follows must not match

(?<=abc)

Positive lookbehind: what precedes must match

(?<=\$)\d+

(?<!abc)

Negative lookbehind: what precedes must not match

Escapes
\n

Line feed

\t

Tab

\r

Carriage return

\f

Form feed

\v

Vertical tab

\0

The NUL character

\cX

A control character, e.g. \cJ for line feed

\\

A literal backslash

\.

Escape a metacharacter, e.g. a literal dot

\u4E2D

Match one character by code point

\u{1F600}

Code point above U+FFFF, needs the u flag

\u{1F600}

\x41

Match one character by hex code

Flags
g

Global: find every match

i

Case-insensitive

m

Multiline: ^ and $ match line boundaries

s

Dot matches line breaks too

u

Treat the pattern as Unicode

v

Unicode sets: adds set operations, cannot be combined with u

[\p{L}--[aeiou]]

y

Sticky: match only at the current position

d

Record match indices

Based on JavaScript (ECMAScript) regular expressions. Lookbehind, \p{...} and the v flag need a reasonably recent browser. Use the copy button to grab a snippet.