Created
September 26, 2021 13:59
-
-
Save HaiBV/ecd13280f367df14050060f6fc003b3c to your computer and use it in GitHub Desktop.
convert string in Vietnamese to Latin
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
const convertTextToLatin = (str) => { | |
if (!str) return ''; | |
str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/gi, 'a'); | |
str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/gi, 'e'); | |
str = str.replace(/ì|í|ị|ỉ|ĩ/gi, 'i'); | |
str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/gi, 'o'); | |
str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/gi, 'u'); | |
str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/gi, 'y'); | |
str = str.replace(/đ/gi, '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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment