How to make this simple regular expression work?

I am trying to replace all spaces inside a string with a hyphen. I tried this:

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

But it replaces only the first copy of the space, and not the one after it. What is a regular expression so that it replaces ALL empty spaces?

+5
source share
1 answer

to try

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

the g flag is used here . this means global replacement, i.e. replace all

+6
source

All Articles