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. public string replace(System.RegExp search, string replacement) public string replace(System.RegExp search, string() replacement) public string replace(System.RegExp search, string(string matched) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups, int offset) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups, int offset, string originalString) replacement) public string replace(string search, string replacement) public string replace(string search, string(string matched) replacement) public string replace(string search, string(string matched, ...string captureGroups) replacement) public string replace(string search, string(string matched, ...string captureGroups, int offset) replacement) public string replace(string search, string(string matched, ...string captureGroups, int offset, string originalString) replacement) Usage public string replace(System.RegExp search, string replacement) public string replace(System.RegExp search, string() replacement) public string replace(System.RegExp search, string(string matched) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups, int offset) replacement) public string replace(System.RegExp search, string(string matched, ...string captureGroups, int offset, string originalString) replacement) public string replace(string search, string replacement) public string replace(string search, string(string matched) replacement) public string replace(string search, string(string matched, ...string captureGroups) replacement) public string replace(string search, string(string matched, ...string captureGroups, int offset) replacement) public string replace(string search, string(string matched, ...string captureGroups, int offset, string originalString) replacement) Returns A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. A new string with all matching patterns replaced with the specified replacement. 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. Parameters search The regular expression pattern to match. replacement A function that returns a replacement string. Parameters search The regular expression pattern to match. replacement A function that returns a replacement string. Parameters search The regular expression pattern to match. replacement A function that returns a replacement string. Parameters search The regular expression pattern to match. replacement A function that returns a replacement string. Parameters search The regular expression pattern to match. replacement A function that returns a replacement string. Parameters search The substring to match. replacement The string to replace the matched substring with. Parameters search The substring to match. replacement A function that returns a replacement string. Parameters search The substring to match. replacement A function that returns a replacement string. Parameters search The substring to match. replacement A function that returns a replacement string. Parameters search The substring to match. replacement A function that returns a replacement string. 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) Performs a regular expression search over the string and replaces the matched pattern(s) using the specified replacement function. The replacement function is required to return a replacement value for the match as its return value. Performs a regular expression search over the string and replaces the matched pattern(s) using the specified replacement function. On each successful match, the matched substring is passed to the replacement function as an argument. The replacement function is required to return a replacement value for the match as its return value. Performs a regular expression search over the string and replaces the matched pattern(s) using the specified replacement function. On each successful match, the matched substring is passed to the replacement function as an argument along with all captured substrings from regular expression capture groups. The replacement function is required to return a replacement value for the match as its return value. Performs a regular expression search over the string and replaces the matched pattern(s) using the specified replacement function. On each successful match, the matched substring is passed to the replacement function as an argument along with (in order): All captured substrings from regular expression capture groups. The offset of the matched substring. The offset for the matched substring is the position of the matched substring (represented with the matched parameter) relative to the original string. For example, if "bc" was matched in "abc", then the offset will be 1. The offset is zero-based. The replacement function is required to return a replacement value for the match as its return value. Performs a regular expression search over the string and replaces the matched pattern(s) using the specified replacement function. On each successful match, the matched substring is passed to the replacement function as an argument along with (in order): All captured substrings from regular expression capture groups. The offset of the matched substring. The original, whole string The offset for the matched substring is the position of the matched substring (represented with the matched parameter) relative to the original string. For example, if "bc" was matched in "abc", then the offset will be 1. The offset is zero-based. The replacement function is required to return a replacement value for the match as its return value. Performs a substring search over the string and replaces the matched substring with the specified replacement string. Only the first matched substring will be replaced. 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) Performs a substring search over the string and replaces the matched substring using the specified replacement function. Only the first matched substring will be replaced. On a successful match, the matched substring is passed to the replacement function as an argument. The replacement function is required to return a replacement value for the match as its return value. Performs a substring search over the string and replaces the matched substring using the specified replacement function. Only the first matched substring will be replaced. On a successful match, the matched substring is passed to the replacement function as an argument. The replacement function is required to return a replacement value for the match as its return value. Additionally, the captureGroups parameter is also available for the replacement function. However, capture groups are unavailable for plain strings so this argument will always be empty. This method overload is provided only for backwards-compatibility with JavaScript. Performs a substring search over the string and replaces the matched substring using the specified replacement function. Only the first matched substring will be replaced. On a successful match, the matched substring is passed to the replacement function as an argument along with the capture groups and the offset of the matched substring relative to the original string. The offset is zero-based. The replacement function is required to return a replacement value for the match as its return value. Although the captureGroups parameter is available for the replacement function, capture groups are unavailable for plain strings so this argument will always be empty. This method overload is provided only for backwards-compatibility with JavaScript. Performs a substring search over the string and replaces the matched substring using the specified replacement function. Only the first matched substring will be replaced. On each successful match, the matched substring is passed to the replacement function as an argument along with (in order): All captured substrings from capture groups (irrelevant). The offset of the matched substring. The original, whole string The offset for the matched substring is the position of the matched substring (represented with the matched parameter) relative to the original string. For example, if "bc" was matched in "abc", then the offset will be 1. The offset is zero-based. Additionally, the captureGroups parameter is available for the replacement function. However, capture groups are unavailable for plain strings so this argument will always be empty. This method overload is provided only for backwards-compatibility with JavaScript. The replacement function is required to return a replacement value for the match as its return value. Examples Basic Usage 1234import System; string abc = "abc";Console.log(abc.replace(/b/, "i")); // "aic" Replace all occurrences of 'b' 1234import System; string abc = "abbbc";Console.log(abc.replace(/b/g, "i")); // "aiiic" Quoting the matched substring 12345import System; string unquoted = "abc";string quoted = unquoted.replace(/.+/, "\"$&\"");Console.log(quoted); Remove all digits 1234567891011import System; string text = "100-200-300";Console.log( text.replace(/\d/g, string() { return ""; })); // Output:// -- Log and transform each character to uppercase 123456789101112131415import System; string abc = "abc";Console.log( abc.replace(/./g, string(string matched) { Console.log("Match: " + matched); return matched.toUpperCase(); })); // Output:// Match: a// Match: b// Match: c// ABC Replace a string with uppercase captured subexpression 1234567891011121314151617import System; string abc = "abc";Console.log( abc.replace(/a(.)c/, string(string matched, ...string captureGroups) { Console.log("Match: " + matched); foreach(string captured in captureGroups) { Console.log("Captured: " + captured); } return captureGroups[0].toUpperCase(); })); // Output:// Match: abc// Captured: b// B Replace a string with uppercase captured subexpression 12345678910111213141516171819import System; string abc = "abc";Console.log( abc.replace(/a(.)c/, string(string matched, ...string captureGroups, int offset) { Console.log("Match: " + matched); Console.log("Offset of match: " + offset.toString()); foreach(string captured in captureGroups) { Console.log("Captured: " + captured); } return captureGroups[0].toUpperCase(); })); // Output:// Match: abc// Offset of match: 0// Captured: b// B Replace a string with uppercase captured subexpression 123456789101112131415161718192021import System; string abc = "abc";Console.log( abc.replace(/a(.)c/, string(string matched, ...string captureGroups, int offset, string originalString) { Console.log("Match: " + matched); Console.log("Offset of match: " + offset.toString()); Console.log("Original string: " + originalString); foreach(string captured in captureGroups) { Console.log("Captured: " + captured); } return captureGroups[0].toUpperCase(); })); // Output:// Match: abc// Offset of match: 0// Original string: abc// Captured: b// B Basic Usage 1234import System; string abc = "abbc";Console.log(abc.replace("b", "i")); // "aibc" Transform the substring "b" to uppercase 1234567891011import System; string abc = "abc";Console.log( abc.replace("b", string(string matched) { return matched.toUpperCase(); })); // Output:// aBc Transform the substring "b" to uppercase 1234567891011import System; string abc = "abc";Console.log( abc.replace("b", string(string matched, ...string captureGroups) { return matched.toUpperCase(); })); // Output:// aBc Transform the substring "b" to uppercase 12345678910111213import System; string abc = "abc";Console.log( abc.replace("b", string(string matched, ...string captureGroups, int offset) { Console.log("Offset of match: " + offset.toString()); return matched.toUpperCase(); })); // Output:// Offset of match: 1// aBc Replace a string with uppercase captured subexpression 123456789101112131415import System; string abc = "abc";Console.log( abc.replace("b", string(string matched, ...string captureGroups, int offset, string originalString) { Console.log("Offset of match: " + offset.toString()); Console.log("Original string: " + originalString); return matched.toUpperCase(); })); // Output:// Offset of match: 1// Original string: abc// aBc Share HTML | BBCode | Direct Link