How to convert a Title to a URL slug in jQuery?

I’m working on an app in CodeIgniter, and I’m trying to make a field on a form dynamically generate the URL slug. What I’d like to do is remove the punctuation, convert it to lowercase, and replace the spaces with hyphens. So for example, Shane’s Rib Shack would become shanes-rib-shack.

Here’s what I have so far. The lowercase part was easy, but the replace doesn’t seem to be working at all, and I have no idea to remove the punctuation:

$("#Restaurant_Name").keyup(function() {
  var Text = $(this).val();
  Text = Text.toLowerCase();
  Text = Text.replace('/\s/g','-');
  $("#Restaurant_Slug").val(Text);  
});

23 Answers
23

Leave a Comment