Skip to content

Instantly share code, notes, and snippets.

@JDMCreator
Created June 4, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDMCreator/af7ecded20602dcf7950091ae7886a21 to your computer and use it in GitHub Desktop.
Save JDMCreator/af7ecded20602dcf7950091ae7886a21 to your computer and use it in GitHub Desktop.
Convert a String to ASCII (Convert accented characters to normal characters and remove others characters)
function toASCII(str){
var newstr = "",
graph_table = {
"\u0132" : "IJ",
"\u0152" : "OE",
"\u00C6" : "AE",
"\u00D8" : "O"
}
str = str.normalize("NFD");
for(var i=0, code, char;i<str.length;i++){
code = str.charCodeAt(i),
char = str.charAt(i);
if(code<128){
newstr += "" + char;
}
else if(char.toUpperCase() != char.toLowerCase()){
var isLowerCase = char.toLowerCase() == char;
char = graph_table[char.toUpperCase()]
if(char){
newstr += isLowerCase ? char.toLowerCase() : char;
}
}
}
return newstr
}
/*
/* Example :
/* toASCII("étalon") // "etalon"
/**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment