Skip to content
Regular expressionsReference

Regex:Replace

Replace the first match in a string.

Details#

Replace the first match in a string. A new string. Only the first match is replaced; if nothing matches, the original text is returned. Evaluation failure raises an error.

Signature#

Luau
regex:Replace(text: string, replacement: string): string

Parameters#

ParameterTypeDescription
textstringOriginal string.
replacementstringReplacement text; standard ECMAScript replacement tokens such as $1 are supported.

Returns#

A new string. Only the first match is replaced; if nothing matches, the original text is returned. Evaluation failure raises an error.

Examples#

Local example

Luau
local pattern = Regex.new("[0-9]+")
print(pattern:Replace("a12 b34", "#")) -- a# b34

Notes#

  • Only the first match is replaced.