replace Method
See Also
exec Method | match Method | RegExp Object | search Method | String Object Methods | test MethodApplies To: String Object
Requirements
Version 1
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. 
Res
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 "A" with "The".
   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."));

解决方案 »

  1.   

    replace 方法
    返回根据正则表达式进行文字替换后的字符串的复制。stringObj.replace(rgExp, replaceText)参数
    stringObj 必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。 rgExp 必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。replaceText 必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。说明
    replace 方法的结果是一个完成了指定替换的 stringObj 对象的复制。 下面任意的匹配变量都能用来识别最新的匹配以及找出匹配的字符串。在需要动态决定替换字符串的文本替换中可以使用匹配变量。字符 含义 
    $$ $ (JScript 5.5 或更新版本) 
    $& 指定与整个模式匹配的 stringObj 的部分。 (JScript 5.5 或更新版本) 
    $` 指定由 $& 描述的匹配之前的 stringObj 部分。 (JScript 5.5 或更新版本) 
    $' 指定由 $& 描述的匹配之后的 stringObj 部分。 (JScript 5.5 或更新版本) 
    $n 捕获的第 n 个子匹配,此处 n 为从1到9的十进制一位数。 (JScript 5.5 或更新版本) 
    $nn 捕获的第 nn 个子匹配,此处 nn 为从01到99的十进制两位数。 (JScript 5.5 或更新版本) 
    如果 replaceText 为函数,对于每一个匹配的子字符串,调用该函数时带有下面的 m+3 个参数,此处 m 是在 rgExp 中捕获的左括弧的个数。第一个参数是匹配的子字符串。接下来的 m 个参数是查找中捕获的全部结果。第 m+2 个参数是在 stringObj 中匹配出现的偏移量,而第 m+3 个参数为 stringObj。结果为将每一匹配的子字符串替换为函数调用的相应返回值的字符串值。Replace 方法更新全局 RegExp 对象的属性。示例
    下面的示例演示了 replace 方法将第一次出现的单词 "The" 替换为单词 "A" 的用法。function ReplaceDemo(){
       var r, re;                    // 声明变量。
       var ss = "The man hit the ball with the bat.\n";
       ss += "while the fielder caught the ball with the glove.";
       re = /The/g;             // 创建正则表达式模式。
       r = ss.replace(re, "A");    // 用 "A" 替换 "The"。
       return(r);                   // 返回替换后的字符串。
    }
      

  2.   

    replace 方法
    返回根据正则表达式进行文字替换后的字符串的复制。stringObj.replace(rgExp, replaceText)参数
    stringObj 必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。 rgExp 必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。replaceText 必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。