fix(match): include last word in capture groups spanning wildcards - #1213
Merged
spencermountain merged 1 commit intoJul 18, 2026
Merged
Conversation
A capture group that wrapped a greedy wildcard together with tokens before it, e.g. `[one .* after]`, dropped the tokens captured before the wildcard (and any trailing literal), returning "one two three" instead of "one two three after". `doAstrix` overwrote the group's length with the wildcard span (`g.length = skipto - state.t`) instead of accumulating onto the length already contributed by earlier regs in the same group. Switch to `g.length += skipto - state.t` so leading tokens are preserved; the trailing literal is then appended by the normal match loop. Closes spencermountain#1139
Owner
|
wow, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1139
Symptom
A named capture group that wraps a greedy
./.*wildcard together with surrounding tokens drops the tokens after (and mis-sizes the span around) the wildcard:Reproduced by @spencermountain in the issue thread.
Root cause
In
src/1-one/match/methods/match/steps/astrix.js,doAstrixsets the group result with:The assignment overwrites whatever length earlier regs in the same group had already contributed, so the group is re-anchored to just the wildcard span. The tokens captured before the wildcard are dropped from the group, and the trailing literal ends up outside the reported range.
Fix
Accumulate instead of overwrite:
Leading tokens stay in the group, and the trailing literal is appended by the normal match loop as before.
Tests
Added
tests/two/match/greedy-capture.test.jscovering the issue's repro plus variants (leading literal + wildcard, wildcard + trailing literal, group starting at the wildcard, and greedy min/max behavior). Full suite passes: 13,419 assertions, 0 failures (npm test).