Skip to content Skip to sidebar Skip to footer

How To Make This Regex Replace Work On All Characters, Not Just The First?

I’m trying to replace all spaces within a string with hyphens. I tried this: h3Text.replace(/\s/, '-'); But it only replaces the first instance of a space and not the ones after

Solution 1:

try

h3Text.replace(/\s/g, '-');

the g flag is key here. it means global replace, ie replace all

Post a Comment for "How To Make This Regex Replace Work On All Characters, Not Just The First?"