Created
August 27, 2012 18:10
-
-
Save alisterlf/3490957 to your computer and use it in GitHub Desktop.
JAVASCRIPT:Remove Accents
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 RemoveAccents(strAccents) { | |
var strAccents = strAccents.split(''); | |
var strAccentsOut = new Array(); | |
var strAccentsLen = strAccents.length; | |
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž'; | |
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz"; | |
for (var y = 0; y < strAccentsLen; y++) { | |
if (accents.indexOf(strAccents[y]) != -1) { | |
strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1); | |
} else | |
strAccentsOut[y] = strAccents[y]; | |
} | |
strAccentsOut = strAccentsOut.join(''); | |
return strAccentsOut; | |
} |
@ArthurMaroulier
FYI: It doesn't cover all letters ('Ø' for example).
"ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž".normalize('NFD').replace(/[\u0300-\u036f]/g, "");
"AAAAAAaaaaaaOOOOOOØoooooøEEEEeeeeðCcÐIIIIiiiiUUUUuuuuNnSsYyyZz"
did you find a way to convert Ø please ?
If your goal is to sort a list of strings, disregarding accents
['André', 'Álister', 'alan'].sort(new Intl.Collator('pt-BR').compare)
Or just to be safe
['André', 'Álister', 'alan'].sort(new Intl.Collator('pt-BR', {sensitivity:'base'}).compare)
Other variant could be..
const removeAccents = (str) => { return str .toLowerCase() .normalize("NFD") .replace(/[\u0300-\u036f]/g, ""); };
Hello! All the above examples are good! However, I put the function at onChange event on an input field and the next character duplicates all the string from the input. Is there any solution to prevent this? Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
O(n) version, ~5x faster, especially if you want add more accents.