JS does not provide methods such as replaceAll. Using a regular table can achieve the Replace effect. If you are interested, check that the following sample JS string contains the replace () method. However, this method only replaces the first matched string. For example:
New Document 《script》var str = "wordwordwordword";var strNew = str.replace("word","Excel");alert(strNew);《script》
If you want to replace all of them, JS does not provide the replaceAll method. You can use a regular table to achieve the effect of Replace:
str.replace(/word/g,"Excel")
G indicates that the global match is executed (search for all matches instead of stopping the first match ).
New Document 《script》function replaceAll(str){if(str!=null)str = str.replace(/word/g,"Excel")return str;}《script》《script》var str = "wordwordwordword";var strNew = str.replace("word","Excel");strNew = replaceAll(str);alert(strNew);《script》
The preceding method is similar to the following:
str.replace(new RegExp("word","gm"),"Excel")
G ).
M.
In addition, you can also add the Stirng object prototype method:
String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2); }
In this way, you can use replaceAll just like using the replace method.
str.replaceAll("word","Excel");
To sum up, there are three methods:
1. str. replace (/oldString/g, newString)
2. str. replace (new RegExp (oldString, "gm"), newString)
3. added the String object Prototype Method replaceAll.