How to replace substring in Javascript?
replace
only replace the first occurrence of the substring.
Use replaceAll
to replace all the occurrence.
var str='------check';
str.replaceAll('-','');
simplest:
str = str.replace(/-/g, "");
str.replace(/\-/g, '');
The regex g flag is global.
Try this instead:
str = str.replace(/-/g, '');
.replace()
does not modify the original string, but returns the modified version.
With the g
at the end of /-/g
all occurences are replaced.