Skip to content

Instantly share code, notes, and snippets.

@anhtran
Last active April 19, 2021 09:37
Show Gist options
  • Save anhtran/65f15495afbd123698d242f10866dc9d to your computer and use it in GitHub Desktop.
Save anhtran/65f15495afbd123698d242f10866dc9d to your computer and use it in GitHub Desktop.
How to remove all accents in Vietnamese string | Xóa dấu trong tiếng Việt làm slug hoặc để tìm kiếm
const removeAccents = (function () {
const letters1 = 'ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂưăạảấầẩẫậắằẳẵặẹẻẽềềểếỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵỷỹý'
const letters2 = 'AAAAEEEIIOOOOUUADIUOaaaaeeeiioooouuadiuoUAAAAAAAAAAAAAEEEEEEuaaaaaaaaaaaaaeeeeeeeEEIIOOOOOOOOOOOOUUUUeeiioooooooooooouuuuUUUYYYYYuuuyyyyy'
const patternLetters = new RegExp('[' + letters1 + ']', 'g')
const lookupLetters = {}
letters1.split('').forEach(function (letter, i) {
lookupLetters[letter] = letters2[i]
})
const letterTranslator = function (match) {
return lookupLetters[match] || match
}
return function removeAccents (input) {
return input.replace(patternLetters, letterTranslator)
}
})()
@lemihthien
Copy link

export const removeAccents = (function () {
  const letters1 = 'ÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬĐÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴáàảãạăắằẳẵặâấầẩẫậđéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵ'
  const letters2 = 'AAAAAAAAAAAAAAAAADEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYaaaaaaaaaaaaaaaaadeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyy'
  const patternLetters = new RegExp('[' + letters1 + ']', 'g')
  const lookupLetters = {}

  letters1.split('').forEach(function (letter, i) {
    lookupLetters[letter] = letters2[i]
  })

  const letterTranslator = function (match) {
    return lookupLetters[match] || match
  }

  return function removeAccents (input) {
    return input.replace(patternLetters, letterTranslator)
  }
})()

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