Skip to content

Instantly share code, notes, and snippets.

@doraeminemon
Forked from jarvisluong/nonAccentVietnamese.js
Last active October 27, 2022 02:58
Show Gist options
  • Save doraeminemon/d7b8dba2fd6f32b3c2c47db22d33404e to your computer and use it in GitHub Desktop.
Save doraeminemon/d7b8dba2fd6f32b3c2c47db22d33404e to your computer and use it in GitHub Desktop.
Converting standard Vietnamese Characters to non-accent ones. Example: hải -> hai
function nonAccentVietnamese(str) {
str = str.replace(/A|Á|À|Ã||Â|||||Ă||||/g, "A");
str = str.replace(/à|á|||ã|â||||||ă|||||/g, "a");
str = str.replace(/E|É|È|||Ê||||/, "E");
str = str.replace(/è|é||||ê||ế|||/g, "e");
str = str.replace(/I|Í|Ì|Ĩ|/g, "I");
str = str.replace(/ì|í|||ĩ/g, "i");
str = str.replace(/O|Ó|Ò|Õ||Ô|||||Ơ||||/g, "O");
str = str.replace(/ò|ó|||õ|ô||||||ơ|||||/g, "o");
str = str.replace(/U|Ú|Ù|Ũ||Ư||||/g, "U");
str = str.replace(/ù|ú|||ũ|ư|||||/g, "u");
str = str.replace(/Y|Ý|||/g, "Y");
str = str.replace(/|ý|||/g, "y");
str = str.replace(/Đ/g, "D");
str = str.replace(/đ/g, "d");
// Some system encode vietnamese combining accent as individual utf-8 characters
str = str.replace(/\u0300|\u0301|\u0303|\u0309|\u0323/g, ""); // Huyền sắc hỏi ngã nặng
str = str.replace(/\u02C6|\u0306|\u031B/g, ""); // Â, Ê, Ă, Ơ, Ư
return str;
}
@hieunt0303
Copy link

BUG: chữ NGUYỄN sẽ k về đc chữ NGUYEN, bạn xem lại nha

@hieunt0303
Copy link

À bạn thiếu global kìa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment