Quantifiers and Greedy vs Lazy MatchingLesson 4.5
Possessive quantifiers and atomic groups to prevent backtracking
possessive quantifier syntax, atomic group, backtracking prevention, performance use cases, Java vs JavaScript support
Possessive: Give Up Captured Characters, Never
Possessive quantifiers (*+ ++ in Java/PCRE) and atomic groups ((?>pattern)) prevent the engine from backtracking into what they have matched.
JavaScript: No Native Possessive
JavaScript does not support possessive quantifiers or atomic groups (as of ES2024). Simulate the behavior by restructuring the pattern to avoid ambiguous overlap, or use a negated character class instead of a wildcard.
// Instead of possessive, use unambiguous pattern
/\d+[^\d]end/.test('123 end') // non-digit separator eliminates ambiguity
