How to use replaceAll() in Javascript.........................?
This question already has an answer here:
You need to do a global replace. Unfortunately, you can't do this cross-browser with a string argument: you need a regex instead:
ss.replace(/,/g, 'nt');
The g
modifer makes the search global.
You need to use regexp here. Please try following
ss.replace(/,/g,”nt”)
g
means replace it globally.
Here's another implementation of replaceAll. Hope it helps someone.
String.prototype.replaceAll = function (stringToFind, stringToReplace) {
if (stringToFind === stringToReplace) return this;
var temp = this;
var index = temp.indexOf(stringToFind);
while (index != -1) {
temp = temp.replace(stringToFind, stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
};
Then you can use it:
var myText = "My Name is George";
var newText = myText.replaceAll("George", "Michael");
链接地址: http://www.djcxy.com/p/16838.html
上一篇: 如何使用JavaScript删除双下划线