Last active
March 19, 2018 00:25
-
-
Save diegolameira/6590b3c13d610eaf559899749d655a89 to your computer and use it in GitHub Desktop.
Remove Accents
This file contains hidden or 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(string) { | |
const special = 'ÀÁÂÃÄÅĄàáâãäåąßÒÓÔÕÕÖØÓòóôõöøóÈÉÊËĘèéêëęðÇĆçćÐÌÍÎÏìíîïÙÚÛÜùúûüÑŃñńŠŚšśŸÿýŽŻŹžżź'; | |
const stripped = 'AAAAAAAaaaaaaaBOOOOOOOOoooooooEEEEEeeeeeeCCccDIIIIiiiiUUUUuuuuNNnnSSssYyyZZZzzz'; | |
return string | |
.split('') | |
.map((letter) => { | |
const index = special.indexOf(letter); | |
return index !== -1 ? stripped[index] : letter; | |
}) | |
.join(''); | |
} | |
function replaceAccents(str) { | |
if (str.search(/[\xC0-\xFF]/g) > -1) { | |
str = str | |
.replace(/[\xC0-\xC5]/g, 'A') | |
.replace(/[\xC6]/g, 'AE') | |
.replace(/[\xC7]/g, 'C') | |
.replace(/[\xC8-\xCB]/g, 'E') | |
.replace(/[\xCC-\xCF]/g, 'I') | |
.replace(/[\xD0]/g, 'D') | |
.replace(/[\xD1]/g, 'N') | |
.replace(/[\xD2-\xD6\xD8]/g, 'O') | |
.replace(/[\xD9-\xDC]/g, 'U') | |
.replace(/[\xDD]/g, 'Y') | |
.replace(/[\xDE]/g, 'P') | |
.replace(/[\xE0-\xE5]/g, 'a') | |
.replace(/[\xE6]/g, 'ae') | |
.replace(/[\xE7]/g, 'c') | |
.replace(/[\xE8-\xEB]/g, 'e') | |
.replace(/[\xEC-\xEF]/g, 'i') | |
.replace(/[\xF1]/g, 'n') | |
.replace(/[\xF2-\xF6\xF8]/g, 'o') | |
.replace(/[\xF9-\xFC]/g, 'u') | |
.replace(/[\xFE]/g, 'p') | |
.replace(/[\xFD\xFF]/g, 'y'); | |
} | |
return str; | |
} | |
const string = 'Diego José Gonçalves Lameira'; | |
console.log(removeAccents(string)); // Diego Jose Goncalves Lameira | |
console.log(replaceAccents(string)); // Diego Jose Goncalves Lameira |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment