Skip to content

fix(ecmascript): support $ substitution patterns in String.prototype.replace/replaceAll - #991

Open
chiliec wants to merge 1 commit into
trynova:mainfrom
chiliec:fix/string-replace-substitution-patterns
Open

chiliec wants to merge 1 commit into
trynova:mainfrom
chiliec:fix/string-replace-substitution-patterns

Conversation

@chiliec

@chiliec chiliec commented Sep 6, 2026 •

Copy link
Copy Markdown

Closes #938.

String.prototype.replace and replaceAll with a string replaceValue ignored $ patterns: "abc".replace("b", "[$&]") returned a[$&]c instead of a[b]c.

Both now go through GetSubstitution when the replacement contains a $. Without a $ they keep using Rust's replace / replacen as before.

get_substitution itself had two bugs that this exposed:

  • A literal character before a $ pattern swallowed the rest of the template, so [$&] was never substituted.
  • $' and $` mixed UTF-16 and UTF-8 offsets and could panic on non-ASCII strings.

It no longer needs the regexp feature gate, since it doesn't use regexes.

Test262 (full run with -u): 20 tests go from FAIL to PASS, none regress. staging/sm/String/replaceAll.js goes from FAIL to CRASH: it now gets past the $$ checks and reaches "αβ".replace(RegExp("", "g"), …), which also crashes on main (empty regex on a non-ASCII string). That's a separate bug and not touched here.

@aapoalas aapoalas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, and welcome to the project.

First - LLM coding is mostly welcome, but I'd rather you write your own PR descriptions and comments.

Second - good job on holding the GC correctly! That's often not easy for first-comers.

Third - please don't change the spec text at all. It might be that there's been a spec update in between now and when you made this, but at least some parts seem to have been editorialised by you. We should purely copy and use the spec text as-is. At most what we can do is leave some of it out, though that is also not generally a good idea.

Fourth - there are some issues around cloning of Strings / Vecs which I'd like to see fixed before this is merged.

Fifth - you have to rerun the full -- -u test suite.

@chiliec
chiliec force-pushed the fix/string-replace-substitution-patterns branch from 3fdfca3 to e72bf47 Compare November 25, 2026 13:21
@chiliec

chiliec commented Nov 25, 2026

Copy link
Copy Markdown
Author

Thanks for the review, and sorry it took me this long to come back to it. I've pushed a new version (e72bf47), rebased on main:

  • Removed the string copies and clones you pointed out.
  • Plain Rust replace is used again when the replacement has no $, in both functions.
  • Pre-sized the replaceAll result as you suggested.
  • Spec comments are now copied as-is from the spec, with the right step numbers. I removed the ones I'd written myself.
  • Ran the full suite with -u. That found three crashes in my previous version: $' on non-ASCII strings, replaceAll with an empty search string on non-ASCII strings, and $` at the end of the string. All fixed now. 20 tests go from FAIL to PASS.

One test, staging/sm/String/replaceAll.js, moves from FAIL to CRASH. It now gets further and hits "αβ".replace(RegExp("", "g"), "x"), which crashes on main as well. I've left that out of this PR; happy to open an issue for it.

On your first point, to be upfront: I used an AI assistant for both the code changes and this write-up. Everything above was checked against the full test262 run before pushing. I've also replaced the PR description with a shorter, up-to-date one.

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.

String.prototype.replace does not work with replacement patterns

2 participants