I need to reformat a string using jQuery or vanilla JavaScript
Let’s say we have "Sonic Free Games".
I want to convert it to "sonic-free-games".
So whitespaces should be replaced by dashes and all letters converted to small letters.
Any help on this please?
Just use the String
replaceandtoLowerCasemethods, for example:Notice the
gflag on theRegExp, it will make the replacement globally within the string, if it’s not used, only the first occurrence will be replaced, and also, thatRegExpwill match one or more white-space characters.