replace_first()

View on GitHub

Syntax

replace_first(string, regex)
replace_first(string, regex, replacement)

Returns a new string with the first match of regex replaced by replacement, or removed if replacement is not specified.

Regex

Regular expressions are based on Java Pattern.

Escaping

Backslashes (\) inside Scarpet strings are interpreted as character escapes. It is necessary to use double backslashes in the regular expression literal to prevent Scarpet from interpreting them. For example '\w' produces an error so '\\w' must be used instead.

Examples

string = 'John\'s dog is cuter than Jane\'s dog';
print(replace_first(string, 'dog', 'cat')); // John's cat is cuter than Jane's dog