Replace all spaces with dashes in Regex Javascript

// How to replace spaces with dashes in JavaScript.
// Notice the g flag on the RegExp, it will make the replacement globally within the string, if it's not used, only the first occurrence will be replaced, and also, that RegExp will match one or more white-space characters.


var str = "How to replace spaces with dashes in JavaScript";
str = str.replace(/\s+/g, '-').toLowerCase();
console.log(str); 

// how-to-replace-spaces-with-dashes-in-javascript

#javascript #RegExp

35.15 GEEK