Regular Expressions

Character Classes

.Any character except newline
\dDigit [0-9]
\DNon-digit
\wWord char [a-zA-Z0-9_]
\WNon-word char
\sWhitespace
\SNon-whitespace
[abc]Any of a, b, c
[^abc]Not a, b, or c
[a-z]Range a to z
[0-9]Range 0 to 9
[a-zA-Z]Any letter

Quantifiers

*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?0+ (lazy / non-greedy)
+?1+ (lazy / non-greedy)

Anchors & Boundaries

^Start of string/line
$End of string/line
\bWord boundary
\BNon-word boundary
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind

Groups & References

(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capture group
\1Back-reference to group 1
a|bAlternation (a or b)
(a)(b)\2\1Multiple back-refs

Special Characters

\\Literal backslash
\.Literal dot
\*Literal asterisk
\+Literal plus
\?Literal question mark
\(\)Literal parentheses
\[\]Literal brackets
\n / \tNewline / tab

Flags

gGlobal (find all matches)
iCase insensitive
mMultiline (^$ per line)
sDotall (. matches \n)
uUnicode support
ySticky (match at index)

Common Patterns

^\S+@\S+\.\S+$Basic email pattern
^https?://.*URL starting with http(s)
^\d{4}-\d{2}-\d{2}$Date YYYY-MM-DD
^\+?[1-9]\d{1,14}$Phone number (E.164)
^#?([0-9a-fA-F]{3}){1,2}$Hex color code
^(?=.*[A-Z])(?=.*\d).{8,}$Strong password
^\d{1,3}(\.\d{1,3}){3}$IPv4 address pattern
\b\w+\bMatch whole words

JS Regex Methods

/pattern/.test(str)Returns true/false
str.match(/pat/g)Array of all matches
str.replace(/pat/, "new")Replace first match
str.replaceAll(/pat/g, "new")Replace all
str.search(/pat/)Index of first match
str.split(/pat/)Split by pattern
/pat/.exec(str)Match with groups
str.matchAll(/pat/g)Iterator of all matches
allprintabledoc.com