Just use the String replace
and toLowerCase
methods, for example:
var str = "Sonic Free Games";
str = str.replace(/\s+/g, '-').toLowerCase();
console.log(str); // "sonic-free-games"
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.