remove accents diatrics in javascript

Remove Accents/Diatrics from String in JavaScript

Accents and Diatrics are phonetic alphabets used to emphasize pronunciation. In many non-english languages, accentuated characters are commonly used to communicate the proper pronunciation of words. But sometimes you may need to remove accents/diatrics from string in your website or application, in case it does not support them. In this article, we will learn how to remove accents/diatrics from string using JavaScript. It is useful if you want to convert accentuated text into Unicode text.


Remove Accents/Diatrics from String in JavaScript

Let us say you have the following string with accents/diatrics.

str = "Crème Brulée"

To remove diatrics and accent marks from the above string, we use normalize() string function available in most web browsers since 2015 (ES6).

Here is the command to remove them using normalize() function.

str.normalize("NFD").replace(/[\u0300-\u036f]/g, "")
> "Creme Brulee"

We use NFD as an argument for normalize() function to decompose letters with diatrics into simpler ones. We also use a regular expression with a range of Unicode characters \u0300 to \u036f to match and replace accentuated characters with similar Unicode ones.

You can also use the following command for the same purpose. It makes use of Unicode property escape feature. In the following example {Diatric} property correctly maps accentuated characters to Unicode character set and performs the transformation.

str.normalize("NFD").replace(/\p{Diacritic}/gu, "")

In this article, we have learnt how to remove accent and diatric marks from strings using JavaScript. It is useful if you want to convert accentuated text into Unicode string.

Also read:

How to Convert JS Array into Object
How to Test for Empty JS Object
How to Convert RGB to Hex and Hex to RGB in JavaScript
How to Store Emoji in MySQL Database
How to Select Nth Row in MySQL Table

Leave a Reply

Your email address will not be published. Required fields are marked *