# TextReplaces (/jsdoc/pixi-vn-json/index/namespaces/TextReplaces)



Manages text replacement handlers that process content enclosed in square brackets (`[key]`).

Handlers are called in the order they were added. For each handler, the current text is scanned
for all `[key]` patterns. If the handler's `validation` matches a key, the handler is
invoked with that key. If the handler returns a string, all occurrences of `[key]` are replaced
with the returned value. After a handler finishes processing the text, the next handler starts
on the updated text.

Each handler specifies whether it runs before or after translation via the `type` field in its
options (defaults to `"before-translation"`).

Example [#example]

```ts
import { TextReplaces } from 'pixi-vn-ink'
import { getCharacterById } from "@drincs/pixi-vn";

// Replace [characterId] with the character's display name
TextReplaces.add(
    (key) => {
        const character = getCharacterById(key)
        return character?.name
    },
    {
        name: "character-name",
        description: "Replaces character IDs with their display names",
        validation: /^[a-z_]+$/,
        type: "after-translation",
    }
)
// "Hello [john], meet [jane]!" -> "Hello John, meet Jane!"
```

Variables [#variables]

* [options](/jsdoc/pixi-vn-json/index/namespaces/TextReplaces/variables/options)

Functions [#functions]

* [add](/jsdoc/pixi-vn-json/index/namespaces/TextReplaces/functions/add)
* [info](/jsdoc/pixi-vn-json/index/namespaces/TextReplaces/functions/info)
* [remove](/jsdoc/pixi-vn-json/index/namespaces/TextReplaces/functions/remove)
* [replace](/jsdoc/pixi-vn-json/index/namespaces/TextReplaces/functions/replace)
