As the name implies, replace function searches a string for a particular substring provided as a function parameter and replaces it with the desired string.
Example No. 1
var tempStr = "replace function is not working";
document.write(str.replace("not working", "working"));
Result tempStr = replace function is working
However one problem with replace function is that it only replaces the first substring found.
Example No. 2
var tempStr = "a1b1c1d1e1f1";
document.write(str.replace("1", "+"));
Result tempStr = a+b1c1d1e1f1
We need something like replaceAll function in this situation. replaceAll function doesnot exists but we have a work around.
Example No. 3
var tempStr = "a1b1c1d1e1f1";
tempStr = tempStr.replace(new RegExp("1", 'g'),"+");
Result tempStr = a+b+c+d+e+f+
No comments:
Post a Comment