Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Mahendran-Balaji/35fbb00c0511b9aaf49df6d6d83da11c to your computer and use it in GitHub Desktop.
Save Mahendran-Balaji/35fbb00c0511b9aaf49df6d6d83da11c to your computer and use it in GitHub Desktop.
Create Slug URL using Javascript
/* Javascript string replace spaces with dash using replace() */
/*
* Allowed only lower case letters (a-z)
* Here, the Regular expression is /\s+/g.
*/
$('#slug-url').bind('keyup blur', function() {
var $th = $(this);
$th.val($th.val().replace(/(\s{2,})|[^a-z ']/g, ' '));
$th.val($th.val().replace(/\s+/g, '-'));
$th.val($th.val().replace(/^\s*/, ''));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment