Last active
March 14, 2019 00:30
-
-
Save tieutantan/36b5d01f183408e217e4190be83aa63f to your computer and use it in GitHub Desktop.
JS - convert Title to Slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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