This fixes it:
let str = "This-is-a-news-item-";
str = str.replace(/-/g, ' ');
alert(str);
There were two problems with your code:
- First, String.replace() doesn’t change the string itself, it returns a changed string.
- Second, if you pass a string to the replace function, it will only replace the first instance it encounters. That’s why I passed a regular expression with the
gflag, for ‘global’, so that all instances will be replaced.