Replace spaces with dashes and make all letters lower-case

JavascriptStringReplace

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Above answer can be considered to be confusing a little. String methods are not modifying original object. They return new object. It must be:

var str = "Sonic Free Games";
str = str.replace(/\s+/g, '-').toLowerCase(); //new object assigned to var str

Solution 3 - Javascript

You can also use split and join:

"Sonic Free Games".split(" ").join("-").toLowerCase(); //sonic-free-games

Solution 4 - Javascript

var str = "Tatwerat Development Team";
str = str.replace(/\s+/g, '-');
console.log(str);
console.log(str.toLowerCase())

Solution 5 - Javascript

If you have lodash in your project, you can try kebabCase

_.kebabCase('Sonic Free Games')

https://lodash.com/docs/4.17.15#kebabCase

Solution 6 - Javascript

A very straightforward approach is to use the JavaScript String replaceAll() method.

"Sonic Free Games".replaceAll(' ', '-').toLowerCase(); //sonic-free-games

For simple replacements like this, a regex is often overkill.

This is simpler and more readable.

However, if you still need a regex, you can use them with this method as well. See the moz docs for an example.

If you need IE support, you'll need a polyfill.

Solution 7 - Javascript

@CMS's answer is just fine, but I want to note that you can use this package: https://github.com/sindresorhus/slugify, which does it for you and covers many edge cases (i.e., German umlauts, Vietnamese, Arabic, Russian, Romanian, Turkish, etc.).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEzzDevView Question on Stackoverflow
Solution 1 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - JavascriptyurinView Answer on Stackoverflow
Solution 3 - JavascriptEesaView Answer on Stackoverflow
Solution 4 - JavascriptAbdo-HostView Answer on Stackoverflow
Solution 5 - Javascriptuser9760669View Answer on Stackoverflow
Solution 6 - Javascriptbabatunde akereleView Answer on Stackoverflow
Solution 7 - JavascriptMatiasView Answer on Stackoverflow