Skip to content

Instantly share code, notes, and snippets.

@jeancroy
Created July 10, 2015 12:07
Show Gist options
  • Save jeancroy/bec22a0a48b1c1ceb510 to your computer and use it in GitHub Desktop.
Save jeancroy/bec22a0a48b1c1ceb510 to your computer and use it in GitHub Desktop.
strip accents
function normalize(str) {
if (!str)return "";
return str.toLowerCase().replace(/[^\u0000-\u007E]/g, function (a) {
return diacriticsMap[a] || a;
});
}
function getDiacriticsMap() {
// replace most common accents in french-spanish by their base letter
//"ãàáäâæ?èéëêìíïîõòóöôœùúüûñç"
var from = "\xE3\xE0\xE1\xE4\xE2\xE6\u1EBD\xE8\xE9\xEB\xEA\xEC\xED\xEF\xEE\xF5\xF2\xF3\xF6\xF4\u0153\xF9\xFA\xFC\xFB\xF1\xE7";
var to = "aaaaaaeeeeeiiiioooooouuuunc";
var diacriticsMap = {};
for (var i = 0; i < from.length; i++) {
diacriticsMap[from[i]] = to[i]
}
return diacriticsMap;
}
var diacriticsMap = getDiacriticsMap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment