javascript的 replace() 方法的使用讲解

String.prototype.replace()
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
The pattern can be a string or a , and the replacement can be a string or a function to be called for each match.
语法:
  str.replace(regexp|substr, newSubStr|function[, flags])

Parameters regexp (pattern)   A  object or literal. The match is replaced by the return value of parameter #2.   substr (pattern)   A  that is to be replaced by newSubStr. It is treated as a verbatim string and is notinterpreted as a regular expression.   newSubStr (replacement)   The  that replaces the substring received from parameter #1. A number of special replacement patterns are supported; see the "" section below.   function (replacement)   A function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are described in the "" section below.     例子:   例1:  

var str = \'Twas the night before Xmas...\'; var newstr = str.replace(/xmas/i, \'Christmas\'); console.log(newstr); // Twas the night before Christmas...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdgjg.html