You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
== reg_replace ==
[[../objects/ms.lang.CastException|ms.lang.CastException]] |- ! scope="row" | Since | 3.2.0 |- ! scope="row" | Restricted |
[[../../Optimizer#OPTIMIZE_DYNAMIC|OPTIMIZE_DYNAMIC]]
[[../../Optimizer#CACHE_RETURN|CACHE_RETURN]]
[[../../Optimizer#NO_SIDE_EFFECTS|NO_SIDE_EFFECTS]] |} === Usages ===
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output might be:
Copy Code
The output would be:
Replaces any occurrences of pattern with the replacement in subject. Back references are allowed. 'replacement' can be a string, or a closure that accepts a found occurrence of 'pattern' and returns the replacement string value.
=== Vital Info ===
{| style="width: 40%;" cellspacing="1" cellpadding="1" border="1" class="wikitable"
|-
! scope="col" width="20%" |
! scope="col" width="80%" |
|-
! scope="row" | Name
| reg_replace
|-
! scope="row" | Returns
| string
|-
! scope="row" | Usages
| pattern, replacement, subject
|-
! scope="row" | Throws
| [[../objects/ms.lang.FormatException|ms.lang.FormatException]][[../objects/ms.lang.CastException|ms.lang.CastException]] |- ! scope="row" | Since | 3.2.0 |- ! scope="row" | Restricted |
No
|-
! scope="row" | Optimizations
| [[../../Optimizer#CONSTANT_OFFLINE|CONSTANT_OFFLINE]] [[../../Optimizer#OPTIMIZE_DYNAMIC|OPTIMIZE_DYNAMIC]]
[[../../Optimizer#CACHE_RETURN|CACHE_RETURN]]
[[../../Optimizer#NO_SIDE_EFFECTS|NO_SIDE_EFFECTS]] |} === Usages ===
reg_replace(pattern, replacement, subject)=== Examples === ====Example 1==== Basic usage Given the following code:
reg_replace('\\d', 'Z', '123abc')
Copy Code1 {{function|reg_replace}}('\\d', 'Z', '123abc')
:ZZZabc====Example 2==== Using backreferences Given the following code:
reg_replace('abc(\\d+)', '$1', 'abc123')
Copy Code1 {{function|reg_replace}}('abc(\\d+)', '$1', 'abc123')
:123====Example 3==== Using backreferences with named captures Given the following code:
reg_replace('abc(?<foo>\\d+)', '${foo}', 'abc123')
Copy Code1 {{function|reg_replace}}('abc(?<foo>\\d+)', '${foo}', 'abc123')
123====Example 4==== Using closure as replacement function Given the following code:
reg_replace('cat|dog', closure(@match) {return array('dog': 'cat', 'cat': 'dog')[@match[0]]}, 'Oscar is a cat. Lucy is a dog.')
Copy Code1 {{function|reg_replace}}('cat|dog', {{keyword|closure}}(@match) {{{keyword|return}} {{function|array}}('dog': 'cat', 'cat': 'dog')[@match[0]]}, 'Oscar is a cat. Lucy is a dog.')
:Oscar is a dog. Lucy is a cat.
Find a bug in this page? Edit this page yourself, then submit a pull request. (Note this page is automatically generated from the documentation in the source code.)