Skip to content

Instantly share code, notes, and snippets.

@aozisik
Created July 8, 2014 10:14
Show Gist options
  • Save aozisik/9c34fd5c904b26d53816 to your computer and use it in GitHub Desktop.
Save aozisik/9c34fd5c904b26d53816 to your computer and use it in GitHub Desktop.
Slugify Javascript Function
// 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;
}
@aozisik
Copy link
Author

aozisik commented Jul 8, 2014

Fonksiyonun içine yollanan string, url içerisinde güvenli bir şekilde kullanılabilecek formata getirilir.

console.log(slugify('test amaçlı bir yazı')); // test-amacli-bir-yazi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment