text = "1/2/3"
result = text.replace("/", "");
I expect the result to be "123" but instead it's "12/3" Why?
text = "1/2/3"
result = text.replace("/", "");
I expect the result to be "123" but instead it's "12/3" Why?
Add the global selection "g" flag and use a regex instead of a string in first parameter.
result = text.replace(/\//g, "");
You can do this with regular expression as argument to replace with global selection.
"1/2/3".replace(/\//g, "")
You can try the below Regexp
"1/2/3".replace(/\//g,"");
This will replace all the /
elements with ""
.
Another way of doing the same:
String.prototype.replaceAll = function(matchStr, replaceStr) {
return this.replace(new RegExp(matchStr, 'g'), replaceStr);
};
var str = "1/2/3";
result = str.replaceAll('/', '');
a = "1/2/3"
a =a.split("/").join("")