Possessive RegExp matching

Beside actual implementation bugs (1), I think that backtracking in RegExp is a footgun, and that having a way to match text possessively would make it easier to write predictable parsers.

It would be nice to have either possessive quantifiers (/a*/ => /a*+/, /a?/ => /a?+/, etc...) or a new p flag that would trigger the possessive mode for all operators.

Both options are backwards compatible AFAICT, since they cause errors in current JS.

Not sure how the polyfill story would play out...


  1. Currently /^(?:ab|[^c])*$/ in irregexp (Chromium/Firefox) goes exponential when fed 'ab'.repeat(n) + 'c', even though it has enough info to know that it won't succeed once it reaches the 'c'.
1 Like