Can anyone help me with this script?
I have to replace the value of a variable in a string (string variables are dynamic)
Here’s an example:
var string = "#sport,#fotogallery,#sport,";
var element = "#sport";
string.replace(element+",","");
alert(string);
First, the
replacefunction returns a string, it does not mutate the variable. So, proper usage is as follows:You should take care to ensure that your input is in the exact format, because if there is no trailing comma after the last value, and you want to replace the last value, then simply using
replacewould fail.