How to convert any text to slug with JavaScript?

Probably you are here to find a way to convert your text string in to a slug. Me too wanted the same and could create the following quick and neat function after reading couple of resources.
function makeSlug(String)
        return String
            .toLowerCase()           //Convert the whole string to lowercase
            .replace(/[^\w ]+/g,'')  //Remove all the non-word characters
            .replace(/ +/g,'-')         //Replace white spaces with hyphens
            .replace(/^-/g,'')          //Remove leading hyphens (caused by leading spaces)
            .replace(/-$/g,'')          //Remove trailing hyphens (caused by trailing spaces)

Then you can call the function with the text as your parameter.

var text = "Can you convert me to a slug?"
var slug = makeSlug(text);

And the result will be a hyphen separated, clean, seo friendly slug url.

Hope this helped :) 


  1. A debt of gratitude is in order for setting aside an ideal opportunity to examine this, I feel unequivocally about it and adoration adapting more on this point. In the event that conceivable, as you pick up aptitude, would you psyche upgrading your online journal with additional data? It is to a great degree supportive for me. msmpeng.exe

  2. All the first class program merchants, for example, Google, Firefox, Apple, Microsoft and so on are supporting HTML5. Amazon a goliath web dealer has likewise begun to help HTML5. javascript

  3. Your article has aroused a ton of positive hobby. I can see why since you have made such a decent showing of making it fascinating.

  4. This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it, Tips and tricks

  5. When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now every time a remark is added I get four emails with the same comment. Is there any approach you’ll be able to remove me from that service? Thanks! How To Fix Video Scheduler Internal Error On Windows