I am using below code to replace , with \n\t
ss.replace(',','\n\t')
and i want to replace all the coma in string with \n so add this ss.replaceAll(',','\n\t')
it din't work..........!
any idea how to get over........?
thank you.
I am using below code to replace , with \n\t
ss.replace(',','\n\t')
and i want to replace all the coma in string with \n so add this ss.replaceAll(',','\n\t')
it din't work..........!
any idea how to get over........?
thank you.
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, '\n\t');
The g
modifer makes the search global.
You need to use regexp here. Please try following
ss.replace(/,/g,”\n\t”)
g
means replace it globally.
Here's another implementation of replaceAll
.
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 like:
var myText = "My Name is George";
var newText = myText.replaceAll("George", "Michael");