var str = ‘topics, initiatives, communities’;
var new_str = str.replace(‘,’,”);
Now, it will replace the first comma only, so I will have this in new_str :
topics initiatives, communities
If you want to replace the original string then assign the return string by replace method to original string, such as :
var str = str.replace(‘,’,”);
This will change the original string.
So, to replace all the commas globally from the string, I will have to use global arguments in the replace function such as :
var new_str = str.replace(/,/g, ”);
And it will return me my desired string i.e. topics initiatives communities
So, never forget to use /g arguments in replace method if you want to replace all the occurrences of the string.