Glossary · Definition
Regex cheat sheet
Quick regex reference. Character classes: <code>\d</code> digit, <code>\w</code> word, <code>\s</code> whitespace. Quantifiers: <code>*</code> 0+, <code>+</code> 1+, <code>?</code> 0-1, <code>{n,m}</code> range. Anchors: <code>^</code> start, <code>$</code> end, <code>\b</code> word-boundary. Groups: <code>(...)</code> capture, <code>(?:...)</code> non-capture, <code>(?<name>...)</code> named.
Definition
Quick regex reference. Character classes: <code>\d</code> digit, <code>\w</code> word, <code>\s</code> whitespace. Quantifiers: <code>*</code> 0+, <code>+</code> 1+, <code>?</code> 0-1, <code>{n,m}</code> range. Anchors: <code>^</code> start, <code>$</code> end, <code>\b</code> word-boundary. Groups: <code>(...)</code> capture, <code>(?:...)</code> non-capture, <code>(?<name>...)</code> named.
What it means
<strong>Character classes</strong>: <code>\d</code> = [0-9], <code>\D</code> = non-digit, <code>\w</code> = [A-Za-z0-9_], <code>\W</code> = non-word, <code>\s</code> = whitespace, <code>\S</code> = non-whitespace, <code>.</code> = any char (except newline by default). <strong>Custom classes</strong>: <code>[abc]</code> any of, <code>[a-z]</code> range, <code>[^abc]</code> negation. <strong>Quantifiers</strong>: <code>*</code> 0+, <code>+</code> 1+, <code>?</code> 0-1, <code>{n}</code> exactly n, <code>{n,m}</code> n to m. Lazy variants: <code>*?</code>, <code>+?</code>. <strong>Anchors</strong>: <code>^</code> string/line start, <code>$</code> string/line end, <code>\b</code> word boundary. <strong>Groups</strong>: <code>(...)</code> capture, <code>(?:...)</code> non-capture, <code>(?<name>...)</code> named, <code>(?=...)</code> lookahead, <code>(?!...)</code> negative lookahead, <code>(?<=...)</code> lookbehind. <strong>Flags</strong>: <code>i</code> case-insensitive, <code>g</code> global, <code>m</code> multiline (^/$ match line bounds), <code>s</code> dotall (. matches newlines), <code>u</code> unicode.
Advertisement
Why it matters
Regex is universally useful but easy to write wrong. Common patterns memorized save time. Always test patterns interactively before deploying. The cheat sheet covers ECMAScript / JavaScript regex; PCRE (PHP, Java, .NET) and Python re have slightly different feature sets — PCRE adds recursive patterns and possessive quantifiers; Python re adds named-group access via dict.
Example
Email (rough): <code>\b[\w.+-]+@[\w-]+\.[\w.-]+\b</code>. URL: <code>https?:\/\/[\w.-]+(?:\/[^\s]*)?</code>. US phone: <code>\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}</code>. IPv4: <code>(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}</code>.
Related free tools
Frequently asked questions
Greedy vs lazy quantifiers?
Greedy (default): match as much as possible, then back off. Lazy: match as little as possible. <code>.*</code> on <code><a><b></code> matches everything; <code>.*?</code> matches just <code><a></code>.
When to use lookahead vs capture group?
Lookahead (<code>(?=...)</code>): zero-width assertion, doesn’t consume characters. Capture group: extracts matched text. Use lookahead for ‘match X but only if followed by Y’ without consuming Y.
Why does my regex behave differently in JavaScript vs Python?
Different dialects. Lookbehind syntax differs slightly; Unicode handling differs (especially with <code>\d</code>, which is ASCII-only in default JS but Unicode-aware in Python). Always test in your target language.