mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2934] [Issue]: Regex "Replace With" mangles backslashes and named-group placeholders (differs from normal regex behavior) #1511
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1511
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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, orl(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):
^.*$with no flags, or any pattern), and set Replace With to:C:\Users\nameC:SERS\NAMEC:\Users\nameOther examples: Replace With
\Large{}producesarge{}; Replace With\Upgrade your accountproducesPGRADE YOUR ACCOUNT. The\U/\Lcase mode is "sticky" and keeps casing everything until a\E.Where it is:
packages/shared/src/utils/regex-replacement.ts,expandRegexReplacement, lines 83-116. Thechar === "\\"branch: e.g. line 91-92 sees\U, setscaseMode = "upper", and advances past the backslash (index += 2). The only way to get a literal\Uis 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/\las 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 outputWhat'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):
(?<first>\w+). Replace With:Hello $<frist>!(note the typofrist).John Smith.Hello $<frist>! SmithHello ! SmithThe 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 inctx.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 nativeString.prototype.replace.Bug 3 —
$<>(empty name) leaks$<>into the outputWhat'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):
(?<g>b). Replace With:$<>abc.a$<>cacWhere it is:
packages/shared/src/utils/regex-replacement.ts:44-51. For$<>,closeIndexequalsindex + 2, so the guardcloseIndex > 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 nativeString.prototype.replace.AI-investigated against v2.0.6; verify the reproduction before fixing.
@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.
@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.