- Instant help with your JavaScript coding problems

Remove accents from a string in JavaScript

Question:
How to remove accents from a string in JavaScript?
Answer:
function toNormalForm(str) {
    return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}

console.log(toNormalForm('Text with ä â ë ü í ő ń')); // Text with a a e u i o n
Description:

Sometimes you need to convert a string to its normalized version that doesn't contain any special letters from languages different from English. French, German, Spanish, Hungarian languages have some special characters (letters with accents) like ä â ë ü í ő ń. To remove all accents in a string using vanilla JavaScript use the normalize function supplemented by a string replace .   

The normalize() method returns the Unicode Normalization Form of the string. Special characters can be represented in two ways. For example, the character "ñ" for example can be represented by either of:

  • The single code point U+00F1 .
  • The code point for "n" U+006E is followed by the code point for the combining tilde U+0303 .

So the solution is to convert all letters with accents to the 2 elements version using the normalize method and then remove all special (accent) element using the replace method.

Share "How to remove accents from a string in JavaScript?"
Related snippets:
Tags:
accents, diacritics, special letters, replace, string, javascript
Technical term:
Remove accents from a string in JavaScript