Skip to content

Instantly share code, notes, and snippets.

@akondas
Forked from mathewbyrne/slugify.js
Last active August 29, 2015 14:10
Show Gist options
  • Save akondas/152c4b263f40bdf7f1b5 to your computer and use it in GitHub Desktop.
Save akondas/152c4b263f40bdf7f1b5 to your computer and use it in GitHub Desktop.
Make friendly url and remove Polish national characters
function slugify(text)
{
var fromChar = [/ą/ig, /ę/ig, /ó/ig, /ś/ig, /ł/ig, /ż/ig, /ź/ig, /ć/ig, /ń/ig];
var toChar = ['a', 'e', 'o', 's', 'l', 'z', 'z', 'c', 'n'];
for ( var i in fromChar )
text = text.replace(fromChar[i], toChar[i]);
return text.toString().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(/-+$/, ''); // Trim - from end of text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment