replace Method (JScript 56)
Returns a copy of a string with text replaced using a regular expression or search string.
stringObj.replace(rgExp, replaceText)
Arguments
- stringObj
Required. The String object or string literal on which to perform the replacement. This string is not modified by the replace method.
- rgExp
Required. An instance of a Regular Expression object containing the regular expression pattern and applicable flags. Can also be a String object or literal. If rgExp is not an instance of a Regular Expression object, it is converted to a string, and an exact search is made for the results; no attempt is made to convert the string into a regular expression.
- replaceText
Required. A String object or string literal containing the text to replace for every successful match of rgExp in stringObj. In JScript 5.5 or later, the replaceText argument can also be a function that returns the replacement text.
Remarks
The result of the replace method is a copy of stringObj after the specified replacements have been made.
Any of the following match variables can be used to identify the most recent match and the string from which it came. The match variables can be used in text replacement where the replacement string has to be determined dynamically.
Characters | Meaning |
---|---|
$$ |
$ (JScript 5.5 or later) |
$& |
Specifies that portion of stringObj that the entire pattern matched. (JScript 5.5 or later) |
$` |
Specifies that portion of stringObj that precedes the match described by $&. (JScript 5.5 or later) |
$' |
Specifies that portion of stringObj that follows the match described by $&. (JScript 5.5 or later) |
$ n |
The nth captured submatch, where n is a single decimal digit from 1 through 9. (JScript 5.5 or later) |
$ nn |
The nnth captured submatch, where nn is a two-digit decimal number from 01 through 99. (JScript 5.5 or later) |
If replaceText is a function, for each matched substring the function is called with the following m + 3 arguments where m is the number of left capturing parentheses in the rgExp. The first argument is the substring that matched. The next m arguments are all of the captures that resulted from the search. Argument m + 2 is the offset within stringObj where the match occurred, and argument m + 3 is stringObj. The result is the string value that results from replacing each matched substring with the corresponding return value of the function call.
The replace method updates the properties of the global RegExp object.
Example
The following example illustrates the use of the replace method to replace the first instance of the word "The" with the word "A."
function ReplaceDemo(){
var r, re; //Declare variables.
var ss = "The man hit the ball with the bat.\n";
ss += "while the fielder caught the ball with the glove.";
re = /The/g; //Create regular expression pattern.
r = ss.replace(re, "A"); //Replace "The" with "A".
return(r); //Return string with replacement made.
}
In addition, the replace method can also replace subexpressions in the pattern. The following example swaps each pair of words in the string.
function ReplaceDemo(){
var r, re; //Declare variables.
var ss = "The rain in Spain falls mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; //Create regular expression pattern.
r = ss.replace(re, "$3$2$1"); //Swap each pair of words.
return(r); //Return resulting string.
}
The following example, which works in JScript 5.5 and later, performs a Fahrenheit to Celsius conversion, illustrates using a function as replaceText. To see how this function works, pass in a string containing a number followed immediately by an "F" (e.g., "Water boils at 212").
function f2c(s) {
var test = /(\d+(\.\d*)?)F\b/g; //Initialize pattern.
return(s.replace
(test,
function($0,$1,$2) {
return((($1-32) * 5/9) + "C");
}
)
);
}
document.write(f2c("Water freezes at 32F and boils at 212F."));
Requirements
Applies To: String Object (JScript 5.6)
See Also
Reference
exec Method (JScript 56)
match Method (JScript 5.6)
RegExp Object (JScript 5.6)
search Method (JScript 5.6)
test Method (JScript 5.6)