Regular Expressions Architecture: Pattern Engines, Quantifiers & ReDoS Prevention
Regular expressions provide formal declarative syntax for text search, tokenization, and lexical validation. Standardized across ECMAScript, PCRE, and Python engines, regex uses finite state automata to match character sequences.
1. Capture Groups & Backreferences
Parentheses (pattern) store matched substrings into numbered capture slots ($1, $2) that can be re-used in replacement strings or referenced inside the pattern itself.
Frequently Asked Questions (FAQ)
What is catastrophic backtracking (ReDoS) in regular expressions?
Catastrophic backtracking occurs when a non-deterministic finite automaton (NFA) regex engine evaluates nested quantifiers on non-matching strings, causing exponential 2^n execution cycles that freeze the browser thread.
What is the difference between capturing and non-capturing groups?
Capturing groups (pattern) remember matching text accessible via indices ($1, $2). Non-capturing groups (?:pattern) group expressions without memorizing sub-matches, improving execution performance.
How do substitution backreferences work?
In replacement strings, $1, $2 reference numbered capture groups, $& references the entire matched substring, and $$ inserts a literal dollar sign.