Skip to content

Instantly share code, notes, and snippets.

@ducdhm
Created October 5, 2019 01:56
Show Gist options
  • Save ducdhm/788e8eed96e8c8fc0c3d1ed40115a572 to your computer and use it in GitHub Desktop.
Save ducdhm/788e8eed96e8c8fc0c3d1ed40115a572 to your computer and use it in GitHub Desktop.
Remove Vietnamese tones
function removeTone(str, isCaseSensitive = false) {
let formatted = isCaseSensitive ? str : str.toLowerCase();
formatted = formatted.replace(/à|á|||ã|â||||||ă|||||/g, 'a');
formatted = formatted.replace(/è|é||||ê||ế|||/g, 'e');
formatted = formatted.replace(/ì|í|||ĩ/g, 'i');
formatted = formatted.replace(/ò|ó|||õ|ô||||||ơ|||||/g, 'o');
formatted = formatted.replace(/ù|ú|||ũ|ư|||||/g, 'u');
formatted = formatted.replace(/|ý|||/g, 'y');
formatted = formatted.replace(/đ/g, 'd');
if (isCaseSensitive) {
formatted = formatted.replace(/À|Á|||Ã|Â||||||Ă|||||/g, 'A');
formatted = formatted.replace(/È|É||||Ê|||||/g, 'E');
formatted = formatted.replace(/Ì|Í|||Ĩ/g, 'I');
formatted = formatted.replace(/Ò|Ó|||Õ|Ô||||||Ơ|||||/g, 'O');
formatted = formatted.replace(/Ù|Ú|||Ũ|Ư|||||/g, 'U');
formatted = formatted.replace(/|Ý|||/g, 'Y');
formatted = formatted.replace(/Đ/g, 'D');
}
return formatted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment