Regex
2026-09-14
•7 min read
JavaScript Regular Expressions Cheat Sheet & Matcher Guide
Master character classes, lookaheads, capture groups, and regex performance optimization.
Regular expressions (Regex) provide powerful pattern matching and textual search-and-replace capabilities across all programming languages.
1. Core Regex Character Classes - `\d`: Match any numeric digit (0-9). - `\w`: Match any word character (A-Z, a-z, 0-9, `_`). - `\s`: Match any whitespace character (space, tab, newline). - `.`: Match any character except newlines. - `[A-Z]`: Match any uppercase character range.
2. Regex Flags in JavaScript - `g` (Global): Match all occurrences throughout the target string. - `i` (Case-Insensitive): Ignore character casing differences. - `m` (Multiline): Anchors `^` and `$` match line beginnings and ends. - `s` (DotAll): Allows dot (`.`) to match newline characters.
3. Lookaheads and Lookbehinds - **Positive Lookahead (`(?=...)`):** Asserts that the expression matches ahead without consuming characters. - **Negative Lookahead (`(?!...)`):** Asserts that the expression does NOT match ahead.
Interactive Tools for This Guide
Frequently Asked Questions
What is catastrophic backtracking in regex?
Catastrophic backtracking occurs when nested quantifiers (like (a+)+) match ambiguous strings, forcing the regex engine to test exponential permutations and freezing the CPU thread.