Regex Cheat Sheet - JavaScript

A comprehensive guide to regular expressions with patterns, examples, and explanations

Basic Metacharacters

.Any character except newline
\Escape character
|Alternation (OR)
^Start of string/line
$End of string/line

Character Classes

[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range from a to z
[a-zA-Z0-9]Alphanumeric characters
\dDigit [0-9]
\DNon-digit [^0-9]
\wWord character [a-zA-Z0-9_]
\WNon-word character [^a-zA-Z0-9_]
\sWhitespace [ \t\n\r\f\v]
\SNon-whitespace [^ \t\n\r\f\v]

Quantifiers

*0 or more
+1 or more
?0 or 1
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Non-greedy 0 or more
+?Non-greedy 1 or more
??Non-greedy 0 or 1
{n,m}?Non-greedy range

Groups and Capturing

(pattern)Capturing group
(?:pattern)Non-capturing group
(?<name>pattern)Named capturing group
\1Backreference to group 1
\k<name>Named backreference

Lookarounds

(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind

Flags/Modifiers

gGlobal - find all matches
iCase insensitive
mMultiline - ^ and $ match line breaks
sDot matches newline
uUnicode mode
ySticky - match from lastIndex

Unicode Properties (with u flag)

\p{L}Any letter
\p{N}Any number
\p{P}Any punctuation
\p{S}Any symbol
\p{Ll}Lowercase letter
\p{Lu}Uppercase letter
\p{Script=Latin}Latin script characters
\P{L}Not a letter (negated)

Escape Sequences

\nNewline
\rCarriage return
\tTab
\vVertical tab
\fForm feed
\0Null character
\xHHHex character
\uHHHHUnicode character
\u{HHHHH}Unicode code point