Regular Expressions
Character Classes
.—Any character except newline
\d—Digit [0-9]
\D—Non-digit
\w—Word char [a-zA-Z0-9_]
\W—Non-word char
\s—Whitespace
\S—Non-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
\b—Word boundary
\B—Non-word boundary
(?=...)—Positive lookahead
(?!...)—Negative lookahead
(?<=...)—Positive lookbehind
(?<!...)—Negative lookbehind
Groups & References
(abc)—Capturing group
(?:abc)—Non-capturing group
(?<name>abc)—Named capture group
\1—Back-reference to group 1
a|b—Alternation (a or b)
(a)(b)\2\1—Multiple back-refs
Special Characters
\\—Literal backslash
\.—Literal dot
\*—Literal asterisk
\+—Literal plus
\?—Literal question mark
\(\)—Literal parentheses
\[\]—Literal brackets
\n / \t—Newline / tab
Flags
g—Global (find all matches)
i—Case insensitive
m—Multiline (^$ per line)
s—Dotall (. matches \n)
u—Unicode support
y—Sticky (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+\b—Match 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