[GH-ISSUE #2934] [Issue]: Regex "Replace With" mangles backslashes and named-group placeholders (differs from normal regex behavior) #1511

Closed
opened 2026-10-21 05:37:39 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2934

Originally assigned to: @SpicyMarinara on GitHub.

The custom regex replacement engine that powers regex scripts (the "Replace With" field) does not behave the way a normal JavaScript regex replacement does in three cases. All three are reproducible with a simple regex script and produce visibly wrong output in chat / prompts.

These all live in packages/shared/src/utils/regex-replacement.ts, used by both the server prompt-side path (packages/server/src/services/regex/regex-application.ts:120) and the client display-side path (packages/client/src/hooks/use-apply-regex.ts:107).


Bug 1 — A literal backslash followed by a letter silently force-changes the case of your text

What's wrong: If your replacement text contains a backslash directly followed by U, L, E, u, or l (e.g. a Windows path, LaTeX, or escaped JSON), the engine treats it as a "change case" command instead of literal text. It eats the backslash and force-uppercases / lowercases the rest of the replacement. There is no hint in the UI that you must type the backslash twice to get a literal one.

How to reproduce (try it yourself):

  1. Create a regex script. Set Find to something that matches your text (e.g. ^.*$ with no flags, or any pattern), and set Replace With to: C:\Users\name
  2. Run it against any matching message.
  3. You'll see: C:SERS\NAME
  4. Expected (what a normal regex replace gives): C:\Users\name

Other examples: Replace With \Large{} produces arge{}; Replace With \Upgrade your account produces PGRADE YOUR ACCOUNT. The \U/\L case mode is "sticky" and keeps casing everything until a \E.

Where it is: packages/shared/src/utils/regex-replacement.ts, expandRegexReplacement, lines 83-116. The char === "\\" branch: e.g. line 91-92 sees \U, sets caseMode = "upper", and advances past the backslash (index += 2). The only way to get a literal \U is to type \\U (the escape at lines 85-89), but the editor help never mentions this.

What's happening: The engine supports SillyTavern-style case transforms (\U...\E, \u, etc.). It scans for a backslash and, if the next char is a case letter, swallows the backslash and switches case mode. Because real replacement text often contains \U, \L, etc. as literal characters, that text gets silently corrupted. The editor "Replace With" help (packages/client/src/components/agents/RegexScriptEditor.tsx:549) documents the case transforms but never says literal backslashes must be doubled.

Suggested fix: Either (a) only treat \U/\L/\E/\u/\l as commands in a way users opt into, or at minimum (b) document in the "Replace With" help that a literal backslash-before-a-letter must be written as \\. Ideally also make the case-command parsing less greedy so common literal text (paths, LaTeX) survives.


Bug 2 — A misspelled / unknown $<name> leaks the literal $<name> into the output

What's wrong: If your replacement references a named capture group that doesn't exist (e.g. a typo), and the Find pattern has at least one named group, the engine prints the literal text $<name> instead of substituting nothing the way a normal regex replace does.

How to reproduce (try it yourself):

  1. Create a regex script. Find: (?<first>\w+) . Replace With: Hello $<frist>! (note the typo frist).
  2. Run against input John Smith.
  3. You'll see: Hello $<frist>! Smith
  4. Expected (normal regex replace): Hello ! Smith

The literal $<frist> ends up in the prompt / rendered message.

Where it is: packages/shared/src/utils/regex-replacement.ts, expandRegexReplacementToken, lines 43-51. When the name isn't found in ctx.groups (line 47), it falls through to line 51 and returns a bare $, then re-scans <frist> as ordinary text.

Note: This only diverges when the Find pattern has at least one named group. With no named groups, native JS also keeps the literal token, so there's no difference there.

Suggested fix: When $<...> is well-formed (has a closing >) but the name is unknown, consume the whole token and substitute empty string, matching native String.prototype.replace.


Bug 3 — $<> (empty name) leaks $<> into the output

What's wrong: An empty named-group placeholder $<> is printed literally instead of being treated as a substitution (empty), unlike a normal regex replace.

How to reproduce (try it yourself):

  1. Create a regex script. Find: (?<g>b) . Replace With: $<>
  2. Run against input abc.
  3. You'll see: a$<>c
  4. Expected (normal regex replace): ac

Where it is: packages/shared/src/utils/regex-replacement.ts:44-51. For $<>, closeIndex equals index + 2, so the guard closeIndex > index + 2 (line 45) is false. The name is never read and line 51 returns a bare $, re-emitting $<> as literal text. This is a separate code path from Bug 2 (the guard fails before any group lookup).

Suggested fix: Treat $<> as a complete token and substitute empty string, consistent with native String.prototype.replace.


AI-investigated against v2.0.6; verify the reproduction before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2934 Originally assigned to: @SpicyMarinara on GitHub. The custom regex replacement engine that powers regex scripts (the "Replace With" field) does not behave the way a normal JavaScript regex replacement does in three cases. All three are reproducible with a simple regex script and produce visibly wrong output in chat / prompts. These all live in `packages/shared/src/utils/regex-replacement.ts`, used by both the server prompt-side path (`packages/server/src/services/regex/regex-application.ts:120`) and the client display-side path (`packages/client/src/hooks/use-apply-regex.ts:107`). --- ### Bug 1 — A literal backslash followed by a letter silently force-changes the case of your text **What's wrong:** If your replacement text contains a backslash directly followed by `U`, `L`, `E`, `u`, or `l` (e.g. a Windows path, LaTeX, or escaped JSON), the engine treats it as a "change case" command instead of literal text. It eats the backslash and force-uppercases / lowercases the rest of the replacement. There is no hint in the UI that you must type the backslash twice to get a literal one. **How to reproduce (try it yourself):** 1. Create a regex script. Set Find to something that matches your text (e.g. `^.*$` with no flags, or any pattern), and set **Replace With** to: `C:\Users\name` 2. Run it against any matching message. 3. You'll see: `C:SERS\NAME` 4. Expected (what a normal regex replace gives): `C:\Users\name` Other examples: Replace With `\Large{}` produces `arge{}`; Replace With `\Upgrade your account` produces `PGRADE YOUR ACCOUNT`. The `\U`/`\L` case mode is "sticky" and keeps casing everything until a `\E`. **Where it is:** `packages/shared/src/utils/regex-replacement.ts`, `expandRegexReplacement`, lines 83-116. The `char === "\\"` branch: e.g. line 91-92 sees `\U`, sets `caseMode = "upper"`, and advances past the backslash (`index += 2`). The only way to get a literal `\U` is to type `\\U` (the escape at lines 85-89), but the editor help never mentions this. **What's happening:** The engine supports SillyTavern-style case transforms (`\U`...`\E`, `\u`, etc.). It scans for a backslash and, if the next char is a case letter, swallows the backslash and switches case mode. Because real replacement text often contains `\U`, `\L`, etc. as literal characters, that text gets silently corrupted. The editor "Replace With" help (`packages/client/src/components/agents/RegexScriptEditor.tsx:549`) documents the case transforms but never says literal backslashes must be doubled. **Suggested fix:** Either (a) only treat `\U`/`\L`/`\E`/`\u`/`\l` as commands in a way users opt into, or at minimum (b) document in the "Replace With" help that a literal backslash-before-a-letter must be written as `\\`. Ideally also make the case-command parsing less greedy so common literal text (paths, LaTeX) survives. --- ### Bug 2 — A misspelled / unknown `$<name>` leaks the literal `$<name>` into the output **What's wrong:** If your replacement references a named capture group that doesn't exist (e.g. a typo), and the Find pattern has at least one named group, the engine prints the literal text `$<name>` instead of substituting nothing the way a normal regex replace does. **How to reproduce (try it yourself):** 1. Create a regex script. Find: `(?<first>\w+)` . Replace With: `Hello $<frist>!` (note the typo `frist`). 2. Run against input `John Smith`. 3. You'll see: `Hello $<frist>! Smith` 4. Expected (normal regex replace): `Hello ! Smith` The literal `$<frist>` ends up in the prompt / rendered message. **Where it is:** `packages/shared/src/utils/regex-replacement.ts`, `expandRegexReplacementToken`, lines 43-51. When the name isn't found in `ctx.groups` (line 47), it falls through to line 51 and returns a bare `$`, then re-scans `<frist>` as ordinary text. **Note:** This only diverges when the Find pattern has at least one named group. With no named groups, native JS also keeps the literal token, so there's no difference there. **Suggested fix:** When `$<...>` is well-formed (has a closing `>`) but the name is unknown, consume the whole token and substitute empty string, matching native `String.prototype.replace`. --- ### Bug 3 — `$<>` (empty name) leaks `$<>` into the output **What's wrong:** An empty named-group placeholder `$<>` is printed literally instead of being treated as a substitution (empty), unlike a normal regex replace. **How to reproduce (try it yourself):** 1. Create a regex script. Find: `(?<g>b)` . Replace With: `$<>` 2. Run against input `abc`. 3. You'll see: `a$<>c` 4. Expected (normal regex replace): `ac` **Where it is:** `packages/shared/src/utils/regex-replacement.ts:44-51`. For `$<>`, `closeIndex` equals `index + 2`, so the guard `closeIndex > index + 2` (line 45) is false. The name is never read and line 51 returns a bare `$`, re-emitting `$<>` as literal text. This is a separate code path from Bug 2 (the guard fails before any group lookup). **Suggested fix:** Treat `$<>` as a complete token and substitute empty string, consistent with native `String.prototype.replace`. --- AI-investigated against v2.0.6; verify the reproduction before fixing.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 28, 2026):

Addressed in the staging fixes: replacement expansion now consumes missing or empty named-group tokens to an empty string when named groups exist, preserving native JS/SillyTavern behavior, while documented case commands still work before captures and literal backslash text such as Windows paths is preserved. Regression coverage was added for these cases.

<!-- gh-comment-id:4827507683 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Addressed in the staging fixes: replacement expansion now consumes missing or empty named-group tokens to an empty string when named groups exist, preserving native JS/SillyTavern behavior, while documented case commands still work before captures and literal backslash text such as Windows paths is preserved. Regression coverage was added for these cases.
Author
Owner

@kolacheee commented on GitHub (Jun 28, 2026):

Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.

<!-- gh-comment-id:4827556831 --> @kolacheee commented on GitHub (Jun 28, 2026): Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1511
No description provided.