Tagscript

ReplaceParser

Swap every occurrence of one string for another.

replace takes what to find and what to put in its place, separated by a comma, and rewrites every match in the payload.

For template authors

Syntax

{replace(find,replacement):text}

Only the first comma splits the two halves. Everything after it is the replacement, commas included, so you can insert a comma but not search for one.

Examples

{replace(o,i):welcome to the server}
# welcime ti the server

{replace(1,6):1637812}
# 6637862

Leave the first half empty to insert the replacement between every character:

{replace(, ):Test}
# T e s t

For developers

Register the parser

import { Interpreter, ReplaceParser } from 'tagscript';
const ts = new Interpreter(new ReplaceParser());

Behaviour

Both the parameter and the payload are required. parse calls String.prototype.replaceAll with plain strings, so the search text is literal and never treated as a regular expression. There is no way for a template author to write a pattern that backtracks.

API reference

ReplaceParser

Last updated on

On this page

Edit on Github