← SillyTavern guides

SillyTavern Regex scripts: clean up and reformat replies

Updated

SillyTavern's Regex extension runs find-and-replace patterns on messages: on what the AI wrote, what you typed, or both. Write the pattern in Find Regex as /pattern/flags, the new text in Replace With, choose what it affects, and decide whether it changes the stored message, only what you see, or only what is sent to the model. It suits mechanical cleanup, whichever API you use, UnsavedAI's included.

Try it now, free, no account neededStart a story with TallowAn ex-demolition robot is the robot city's only detective. Its first murder came the night you landed.

Where scripts live and what the fields do

Open the Extensions panel and find Regex. Global scripts apply to every chat; scoped scripts belong to one character and travel with its card, which is useful when only one card has a formatting habit. Create a script and you get these fields:

FieldWhat it does
Find RegexThe pattern, written like /pattern/flags, with g for every match and i to ignore case
Replace WithThe replacement; $1 inserts the first captured group, {{match}} the whole match
Trim OutText removed from each match before replacing
AffectsUser input, AI output, slash commands, World Info, reasoning
Only Format DisplayChanges what you see, not the saved text
Only Format PromptChanges what is sent to the model, not the saved text
Min / Max DepthLimits the script to messages within a range from the newest (0 is the latest)

Four scripts to copy

1. Strip a name prefix. Some replies open with the character's name and a colon, which then gets copied forward.

  • Find Regex: /^\s*{{char}}:\s*/
  • Replace With: leave empty
  • Affects: AI output, with macros in the pattern substituted so {{char}} becomes the name

2. Straighten curly quotes so every reply matches your formatting.

  • Find Regex: /[“”]/g
  • Replace With: "
  • Affects: AI output

3. Drop a stock closing question that ends every reply.

  • Find Regex: /\s*What (?:do|will) you do(?: next)?\?\s*$/i
  • Replace With: leave empty
  • Affects: AI output

4. Keep a tracker for the model, hide it from your eyes. Say the character ends each reply with [Stats: HP 12/20 | Gold 40]. You want the model to keep reading it, but not to see it under every paragraph.

  • Find Regex: /\[Stats:[^\]]*\]/g
  • Replace With: leave empty
  • Affects: AI output, with Only Format Display ticked

Flip the idea for long games: tick Only Format Prompt instead and set Min Depth to 1, so the latest stats block is still sent but older ones are stripped from the prompt. Every old block is then gone from what the model reads, while all of them stay visible in your chat.

Stored text, display or prompt

This is the choice that trips people up. With neither "only" box ticked, a script on AI output edits the message itself as it arrives, permanently. With Only Format Display, the saved message keeps the original text and you see the cleaned version. With Only Format Prompt, you see the original and the model receives the cleaned version.

Start new scripts as display-only, read a few replies, then decide whether to make the change permanent. The editor has a test area where you paste sample text and watch the result before saving.

Regex cleans; it does not teach

A script that deletes "What do you do?" hides the habit; the model still wants to write it, and sometimes rephrases around your pattern. If you are fighting the same phrase in every reply, also fix the cause in the card or prompt, and use Regex for what really is mechanical: stray name tags, quote styles, leftover markup, tracker blocks.

Prompt-only scripts that touch just the newest messages also leave the earlier history byte-for-byte unchanged, and UnsavedAI reuses an unchanged prompt start between replies, so long chats stay quick. A prompt-only script that rewrites every old message each turn throws that away.

Formatting-heavy cards are where scripts earn their keep: a robot narrator like Tallow or a station mystery with Otto Ferreira can carry status lines and case notes that read better hidden.

Common mistakes

  • Forgetting the g flag, so only the first match changes.
  • Greedy .* swallowing half the reply; use [^\]]* or a lazy .*? instead.
  • Leaving brackets, dots and question marks unescaped.
  • Ticking user input by accident, so your own messages get rewritten.
  • Writing patterns for another regex flavour; SillyTavern uses JavaScript's.

Questions people ask

Can a regex script undo changes it already made?

Not to stored text. If a script without a display or prompt option edited messages, disable it and edit those messages by hand, or swipe again.

Do scoped scripts export with the character?

Yes, scripts scoped to a character are saved inside the card. Read them after importing someone else's card, since they rewrite text.

Characters for this story