Can someone help me with replacing consecutive spaces with a hyphen? For example, I need:
123 321
to become
123-321
Thanks in advance!
var result = "123 321".replace(/ +/g, "-");
console.log(result);
/ +/g = at least 1 space, look globally (in the whole string)
The regular expression \s+ will match any number of consecutive spaces (including tabs and other whitespace characters). Use that as a global pattern for string.replace().
Example from a Javascript console:
> "a b".replace(/\s+/g, "-")
"a-b"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With