Regex:Replace Synchronous

Replace the first matching substring and return the resulting text. Use it for a single targeted edit, such as replacing the first version number in a label. Later matches stay unchanged.

Syntax
Luau
Regex:Replace(contents: string, replacement: string) -> string

Parameters

Call this method with a Regex receiver. The colon supplies self implicitly; a dot call must pass the receiver explicitly.

Function parameters
ParameterTypeDescription
contentsstringText to search and transform.
replacementstringReplacement text using Java Matcher replacement syntax; group references can use $1, $2, and so on.

Returns

string

Text with the first match replaced, or the original text when there is no match.

Usage notes

This implementation calls replaceFirst; it does not replace every occurrence.

Example

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