replace

Summary

Performs a regular expression search over the string and replaces the matched pattern(s) with the specified replacement string.

Signatures

Click on a signature to select it and view its documentation.

Usage

public string replace(System.RegExp search, string replacement)

Returns

A new string with all matching patterns replaced with the specified replacement.

Parameters

search

The regular expression pattern to match.

replacement

The string to replace matches with.

Description

Performs a regular expression search over the string and replaces the matched pattern(s) with the specified replacement string.

In addition, the replacement string allows the following substitutions:

Characters Substituted Text
$$ $
$& The matched substring
$` The substring preceding the matched substring
$' The substring following the matched substring
$n The nth capture group (must be 1-9)
$nn The nnth capture group (must be two digits, 01-99)

Examples

Basic Usage
1
2
3
4
import System;
 
string abc = "abc";
Console.log(abc.replace(/b/, "i")); // "aic"
Replace all occurrences of 'b'
1
2
3
4
import System;
 
string abc = "abbbc";
Console.log(abc.replace(/b/g, "i")); // "aiiic"
Quoting the matched substring
1
2
3
4
5
import System;
 
string unquoted = "abc";
string quoted = unquoted.replace(/.+/, "\"$&\"");
Console.log(quoted);

Share

HTML | BBCode | Direct Link