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

  6. The time has come again to compile some of the Best Budget Laptops models for this month. At first glance, I wondered why the DELL is doing so well in laptop market.

  7. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Webdesining course in chennai

  8. The G1W comes with an integrated 2.7" screen, which is great for adjusting the field of view and playing back video on the spot. The viewing angle is 120 degrees Furqan Shahid

  9. Practice Problem Solving A developing class of diversions will constrain your youngster to take care of issues as they play.AD List 24 Classifieds

  10. Android is a Linux based open platform, certainly for mobile devices that includes an operating system and some key mobile applications latest technology