Add tests for nullable quantifiers in RegExps #4185
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The JavaScript regex quantifiers (star, plus, counted repetition) have unique semantics when it comes to matching the empty string.
Optional iterations of quantifiers that match the empty string are forbidden.
This is documented in ECMAScript (point 2.b)
and the PLDI2024 paper Linear Matching of JavaScript Regular Expressions.
In many other regex languages however, the quantifier semantics are different.
Instead of only checking at the end of an iteration, it is forbidden to visit any part of the regex twice without having consumed a character.
In some rare cases, the two semantics give different results.
For instance, matching /(a?b??)*/ on string "ab" matches "ab" in JavaScript (two iterations of the star, each matching one character), but only "a" in other languages.
See for instance https://regex101.com/r/iuVSat/1
I don't believe this JavaScript-specific behavior has been documented in Test262. I suggest adding a test case for this.
Many classical algorithms for regex matching (like NFA simulation or lazy DFA) typically do not implement the JavaScript quantifier semantics.
This led to a bug in the linear engine of V8 (which uses NFA simulation):
Bug Report, Relevant test case in V8, V8 fix, Explanation of the fix (section 4.1).