Skip to content

replace_regex

Replaces characters within a string based on a regular expression.

replace_regex(x:string, pattern:string, replacement:string, [max=int]) -> string

The replace_regex function returns a new string where substrings in x that match pattern are replaced with replacement, up to max times. If max is omitted, all matches are replaced.

The subject to replace the action on.

The pattern (as regular expression) to replace in x.

The replacement value for pattern.

The maximum number of replacements to perform.

If the option is not set, all occurrences are replaced.

Replace all matches of a regular expression

Section titled “Replace all matches of a regular expression”
from {x: replace_regex("hello", "l+", "y")}
{x: "heyo"}
from {x: replace_regex("hellolo", "l+", "y", max=1)}
{x: "heyolo"}

replace