Leo's dev blog

Replace all VNese letters with the corresponding English accents

Published on
Published on
/1 mins read/---

Replace all Vietnamese letters with the corresponding English accents

vnese-to-english.js
let toPlainEnglish = (str) => {
  str = str.replace(|á|||ã|â||||||ă|||||ẵ/g, 'a')
  str = str.replace(|é||||ê||ế|||ễ/g, 'e')
  str = str.replace(|í|||ĩ/g, 'i')
  str = str.replace(|ó|||õ|ô||||||ơ|||||ỡ/g, 'o')
  str = str.replace(|ú|||ũ|ư|||||ữ/g, 'u')
  str = str.replace(/ỳ|ý|||ỹ/g, 'y')
  str = str.replace(/đ/g, 'd')
  str = str.replace(|Á|||Ã|Â||||||Ă|||||Ẵ/g, 'A')
  str = str.replace(|É||||Ê|||||Ễ/g, 'E')
  str = str.replace(|Í|||Ĩ/g, 'I')
  str = str.replace(|Ó|||Õ|Ô||||||Ơ|||||Ỡ/g, 'O')
  str = str.replace(|Ú|||Ũ|Ư|||||Ữ/g, 'U')
  str = str.replace(/Ỳ|Ý|||Ỹ/g, 'Y')
  str = str.replace(/Đ/g, 'D')
 
  // OPTIONAL - Remove special chars
  // str = str.replace(/[^a-zA-Z0-9 \s]/g, "")
 
  return str
}
 
console.log(toPlainEnglish('Tuấn Anh')) // => "Tuan Anh"