Skip to content

fix: expand special replacement patterns for regexp find - #118

Open
tlq5l wants to merge 1 commit into
unplugin:mainfrom
tlq5l:fix/regexp-replacement-patterns-117
Open

tlq5l wants to merge 1 commit into
unplugin:mainfrom
tlq5l:fix/regexp-replacement-patterns-117

Conversation

@tlq5l

@tlq5l tlq5l commented Nov 24, 2026

Copy link
Copy Markdown

Summary

A string replacement for a RegExp find now expands $1, $& and the other special replacement patterns, as the replacement docs say. Before, { find: /process\.env\.(\w+)/g, replacement: 'import.meta.env.$1' } produced import.meta.env.$1.

The expansion follows String.prototype.replace: $$, $&, $`, $', $1 to $99 and $<name>; $` and $' read the original code, comments included. String keys (the object form and string find) and function replacements are unchanged.

A RegExp replacement that relied on $ followed by one of those tokens staying literal now gets the expanded text; $$ gives a literal $.

Fixes #117

Tests

New tests in tests/rollup.test.ts: find regexp with special replacement patterns, and find regexp with $' keeps comments.

Fails without fix: pnpm install --frozen-lockfile && NO_COLOR=1 pnpm vitest run tests/rollup.test.ts on 5b00fec, with the fix's code reverted to origin/main: exit 1, failing: find regexp with special replacement patterns; find regexp with $' keeps comments; rollup > find regexp with special replacement patterns.

Tests (agent-run) at 5b00fec, clean setup (empty HOME, no git config): pnpm install --frozen-lockfile && NO_COLOR=1 pnpm vitest run tests/rollup.test.ts, exit 0. Last output line: Duration 275ms (import 49%, tests 43%, worker 8%).

AI disclosure: this PR was written with an AI coding agent; the test results above come from the agent's runs.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$1, $& in string replacements for RegExp find are not expanded

1 participant