Created
July 8, 2014 10:14
-
-
Save aozisik/9c34fd5c904b26d53816 to your computer and use it in GitHub Desktop.
Slugify Javascript Function
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
// creates valid slugs from strings. replaces turkish characters correctly. | |
function slugify(string) { | |
var letters = { "İ":"i", "I":"i", "ı":"i", "Ş":"s", "ş":"s", "Ğ":"g", "ğ":"g", "Ü":"u", "ü":"u", "Ö":"o", "ö":"o", "Ç":"c", "ç":"c" }; | |
string = string.replace(/[İIıŞşĞğÜüÇçÖö]/g, function(letter){ return letters[letter]; }) | |
.toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fonksiyonun içine yollanan string, url içerisinde güvenli bir şekilde kullanılabilecek formata getirilir.