Function: replace()
> replace(text, replaceOptions): string
Defined in: src/handlers/TextReplaces.ts:146
Applies all registered handlers of the specified type to the given text in order.
For each handler:
- All
[key]tokens currently present in the text are collected. - For each unique key, the handler's
validationis tested against the key. - If validation passes, the handler is called with the key.
- If the handler returns a string, all occurrences of
[key]in the text are replaced with that string. - After the handler has processed all tokens, the next handler starts on the updated text.
Parameters
text
string
The source text to process.
replaceOptions
Specifies which phase of handlers to run.
type
"after-translation" | "before-translation"
Which phase of handlers to execute.
Returns
string
The text after all matching handlers have been applied.
Example
// Given handlers that replace [name] -> "Mario" and [surname] -> "Rossi":
TextReplaces.replace("Ciao [name] [surname]. [name] vai pure", { type: "before-translation" })
// => "Ciao Mario Rossi. Mario vai pure"