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.
Luau
Regex:Replace(contents: string, replacement: string) -> stringParameters
Call this method with a Regex receiver. The colon supplies self implicitly; a dot call must pass the receiver explicitly.
| Parameter | Type | Description |
|---|---|---|
contents | string | Text to search and transform. |
replacement | string | Replacement text using Java Matcher replacement syntax; group references can use $1, $2, and so on. |
Returns
stringText 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
Luau
local digits = Regex.new("[0-9]+")
print(digits:Replace("a12 b34", "#")) -- a# b34