Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Last active March 14, 2019 00:30
Show Gist options
  • Save tieutantan/36b5d01f183408e217e4190be83aa63f to your computer and use it in GitHub Desktop.
Save tieutantan/36b5d01f183408e217e4190be83aa63f to your computer and use it in GitHub Desktop.
JS - convert Title to Slug
<script>
// title to slug
function title_to_slug(title_id, slug_id)
{
string = document.getElementById(title_id).value;
string = string.toString().trim().toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\-]+/g, "")
.replace(/[0-9]/g, "")
.replace(/-that|-keyword|-need|-to|-remove|-from|-slug/gi, "")
.replace(/\-\-+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "")
;
document.getElementById(slug_id).value = string;
}
</script>
<input onkeyup="title_to_slug('title', 'slug');" id="title" type="text">
<input id="slug" type="text">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment