tutorialApril 21, 20264 min read

Regex for Roleplay Formatting: What Actually Sticks

A practical guide to making output formatting consistent without wasting context on fragile prompt tricks.


You can spend hours building a character voice and still watch the output land wrong.

Dialogue loses quotes. Actions drift. Halfway through a session the model starts prefixing everything with Assistant: again.

Immersion shatters immediately.

I used to fight this with system prompts.

It never holds.

Models drift as context grows. And every “formatting reminder” you paste into Creator Notes steals tokens from continuity.

If a rule must hold forever, do not ask the model politely.

Enforce it mechanically.

That is where Regex (Regular Expressions) becomes useful.

A high-contrast black and white minimalist photograph of a chaotic tangle of black wires suddenly passing through a solid steel ring and emerging as perfectly straight, parallel lines.

TL;DR - The Approach

  • Prompts are suggestions.
  • Post-processing is enforcement.
  • Keep the rule set small, or you will create a new maintenance problem.

1. The Anatomy of Regex in AI Roleplay

Regex is a sequence of characters that specifies a search pattern. In the context of AI frontends, it functions as a microscopic find-and-replace engine. It scans the incoming text stream from the LLM, identifies specific structural patterns, and rewrites them in real-time.

To manipulate chat outputs, you only need to understand three core components:

  • Literal Characters: The exact text you want to find (e.g., *, ", AI:).
  • Wildcards and Quantifiers: Symbols that represent variable text. . means "any character," and * means "zero or more times." Therefore, .* translates to "literally anything."
  • Capture Groups: Wrapping a section of your regex in parentheses () saves the text found inside that specific pattern so you can reuse it in the replacement string (referred to as $1, $2, etc.).

2. Essential Regex Snippets (Copy & Paste)

These are the foundational transforms I keep reusing. They are not “magic.” They are just deterministic.

Fix 1: Forcing Italics on Asterisk Actions

Some models output actions wrapped in asterisks, but your renderer does not always interpret them the way you expect. This rule forces consistency.

  • Target Pattern: \*([^*]+)\*
  • Replacement: <i>$1</i>

How it works: The \* looks for a literal asterisk. The ([^*]+) is a capture group that grabs every character until it hits the next asterisk. The replacement wraps that captured text ($1) in standard HTML tags.

Fix 2: Stripping Annoying AI Prefixes

Some datasets teach the model to label its role. Your UI usually does not need the label.

  • Target Pattern: ^(?:Character|AI|Assistant):\s*
  • Replacement: (Leave blank)

How it works: The ^ asserts the start of the line. The (?:...) looks for specific words. The \s* catches any trailing spaces. Replacing it with nothing deletes the prefix entirely, leaving only the actual dialogue.

Fix 3: Standardizing Dialogue Quotes

If a model mixes multiple quote styles, normalize and move on.

  • Target Pattern: [“”「」]
  • Replacement: "

A stark black and white photograph of a heavy guillotine blade suspended perfectly still above a single sheet of white paper.


3. Native Formatting vs. Custom Control

Regex is powerful, and that is exactly why it can turn into a trap. A giant library of rules becomes its own maintenance burden.

My rule is: add a transform only when it solves a problem you actually notice in your own sessions.

Disclosure: I build Abolitus.

This friction—control versus fatigue—was a primary focus while building it.

Instead of forcing users to build their formatting rules from scratch, Abolitus handles output formatting through a dual-tier architecture:

  • Tier 1: Native Roleplay Formatting: Out of the box, Abolitus natively understands standard roleplay syntax. Basic formatting—like italicizing asterisk-bound actions or standardizing quotation marks—is built directly into the client engine. You get perfectly formatted, readable output on day one without touching a single line of code.
  • Tier 2: The Advanced Regex Engine: For power users pushing specific aesthetics or dealing with highly experimental local models, Abolitus provides a robust, fully customizable Regex interface.

Because Abolitus is a true local-first client, your custom rules execute strictly via Client-Side Processing. Drop your custom scripts into the UI, and your browser's CPU handles the text replacement instantly as the text streams in from your OpenRouter or Ollama endpoint. No server-side latency, no formatting lag.

If you want a client where formatting rules are enforced deterministically (and the defaults are not hidden on a server), you can try Abolitus.

Continue Reading

Related Guides

Ready for private AI?

Experience zero-log, client-side encrypted AI roleplay directly in your browser.

Launch App