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#
regex:Replace(text: string, replacement: string): stringParameters#
| Parameter | Type | Description |
|---|---|---|
text | string | Original string. |
replacement | string | Replacement 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
local pattern = Regex.new("[0-9]+")
print(pattern:Replace("a12 b34", "#")) -- a# b34Notes#
- Only the first match is replaced.