Skip to main content
Version: v4.24

replace_regex

Replaces characters within a string based on a regular expression.

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

Description

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.

x: string

The subject to replace the action on.

pattern: string

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

replacement: string

The replacement value for pattern.

max = string (optional)

The maximum number of replacements to perform.

Examples

Replace all matches of a regular expression

from {x: replace_regex("hello", "l+", "y")}
{x: "heyo"}

Replace a limited number of matches

from {x: replace_regex("hellolo", "l+", "y", max=1)}
{x: "heyolo"}

See Also

replace