Convert All Spaces in String to Dashes and Make it Lowercase with JavaScript

This will take your JavaScript string and make it lowercase, and convert any spaces to dashes (-).

This is a decent way to “slugify” a string, or make a URL-friendly slug of a string, only if you’re sure that the string will not contain any other special characters such as commas, periods, etc. This will not remove commas or periods from the string.

var string = 'THIS IS SOME STRING';
var slug = string.replace(/\s+/g, '-').toLowerCase();

See more:

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]