stringObj.replace(rgExp, replaceText)
Parameters
stringObj
Required. The String object or string literal to perform this replacement on. The string will not be modified by the replace method.
rgExp
Required. Is a regular expression object containing a regular expression pattern or available flags. Can also be a String object or literal. If rgExp is not a regular expression object, it will be converted to a string and an exact search performed; do not attempt to convert the string to a regular expression.
replaceText
Required. Is a String object or string literal that replaces each matching position in rgExp in stringObj with the literal contained in the object. In Jscript 5.5 or newer, the replaceText parameter can also be a function that returns replacement text.
Explanation
The result of the replace method is a copy of the stringObj object that has completed the specified replacement.
Any of the following match variables can be used to identify the latest match and find the matching string. Match variables can be used in text replacements where the replacement string needs to be determined dynamically.
Character Meaning
$$ $ (JScript 5.5 or newer)
$& specifies the portion of stringObj that matches the entire pattern. (JScript 5.5 or newer)
$` specifies the part of stringObj preceding the match described by $&. (JScript 5.5 or newer)
$' specifies the part of stringObj after the match described by $&. (JScript 5.5 or newer)
$n captures the nth submatch, where n is a decimal digit from 1 to 9. (JScript 5.5 or newer)
$nn captures the nnth submatch, where nn is a decimal two-digit number from 01 to 99. (JScript 5.5 or newer)
If replaceText is a function, for each matched substring, the function is called with the following m 3 parameters, where m is the number of left brackets captured in rgExp number. The first parameter is the substring to match. The next m parameters are all results captured in the search. The m 2nd parameter is the offset within stringObj where the match occurs, and the m 3rd parameter is stringObj. The result is a string value with each matching substring replaced with the corresponding return value of the function call.
The Replace method updates the properties of the global RegExp object.
Example
The following example demonstrates the use of the replace method to replace the first occurrence of the word "The" with the word "A".