Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Created April 28, 2011 18:57
Show Gist options
  • Save Majkl578/947036 to your computer and use it in GitHub Desktop.
Save Majkl578/947036 to your computer and use it in GitHub Desktop.
webalizing string in JS
<head>
<script type="text/javascript">
var webalize = function (str) {
var charlist;
charlist = [
['Á','A'], ['Ä','A'], ['Č','C'], ['Ç','C'], ['Ď','D'], ['É','E'], ['Ě','E'],
['Ë','E'], ['Í','I'], ['Ň','N'], ['Ó','O'], ['Ö','O'], ['Ř','R'], ['Š','S'],
['Ť','T'], ['Ú','U'], ['Ů','U'], ['Ü','U'], ['Ý','Y'], ['Ž','Z'], ['á','a'],
['ä','a'], ['č','c'], ['ç','c'], ['ď','d'], ['é','e'], ['ě','e'], ['ë','e'],
['í','i'], ['ň','n'], ['ó','o'], ['ö','o'], ['ř','r'], ['š','s'], ['ť','t'],
['ú','u'], ['ů','u'], ['ü','u'], ['ý','y'], ['ž','z']
];
for (var i in charlist) {
var re = new RegExp(charlist[i][0],'g');
str = str.replace(re, charlist[i][1]);
}
str = str.replace(/[^a-z0-9]/ig, '-');
str = str.replace(/\-+/g, '-');
if (str[0] == '-') {
str = str.substring(1, str.length);
}
if (str[str.length - 1] == '-') {
str = str.substring(0, str.length - 1);
}
return str.toLowerCase();
}
alert(webalize('_Příliš žluťoučký kůň úpěl ďábelské ódy.'));
</script>
</head>
<body></body>
</html>
@ZbyRih
Copy link

ZbyRih commented Jun 21, 2024

str.toLowerCase()
		.normalize('NFD')
		.replace(/[\u0300-\u036f]/g, "");

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