-
-
Save akondas/152c4b263f40bdf7f1b5 to your computer and use it in GitHub Desktop.
Make friendly url and remove Polish national characters
This file contains 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
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