Regex Cheat Sheet for Everyday Debugging
Regular expressions are one of those tools where a 20-minute investment saves hours every month. Here's the subset that covers 90% of real-world use.
Character classes
\d— digit ([0-9])\w— word char ([A-Za-z0-9_])\s— whitespace[^...]— negation.— any character except newline (addsflag to include newline)
Anchors
^— start of string (or line in multiline mode)$— end of string (or line in multiline mode)\b— word boundary
Quantifiers
*— 0 or more+— 1 or more?— 0 or 1{n}/{n,m}— exact count / range- Add
?to any quantifier for lazy matching (shortest match)
Groups and lookarounds
(abc)— capturing group(?:abc)— non-capturing group(?<name>abc)— named group (ES2018+)(?=abc)— positive lookahead(?!abc)— negative lookahead(?<=abc)— positive lookbehind(?<!abc)— negative lookbehind
Patterns you'll actually use
# Email-ish (pragmatic, not RFC-correct)
[\w.+-]+@[\w-]+\.[\w.-]+
# URL-ish
https?://[\w.-]+(?:/[\w\-./?%&=]*)?
# IPv4
\b(?:\d{1,3}\.){3}\d{1,3}\b
# Semver
\b\d+\.\d+\.\d+(?:-[\w.-]+)?\b
Flags
g— global (find all matches)i— case-insensitivem— multiline (^$match line boundaries)s— dot matches newlineu— unicode
Test your patterns interactively with our regex tester before committing them to code.