HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Remove accents from a JavaScript string

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptfromremoveaccentsstring

Problem

Oftentimes you might need to remove accents from a string in JavaScript. This can be useful when you want to compare strings without considering accents, or when you want to normalize strings for storage or display.
This might seem like a difficult task, but it's actually quite simple. As a matter of fact, JavaScript's String.prototype.normalize() method makes it a breeze, given its 'NFD' option. This option allows you to convert the string to a normalized Unicode format, which separates the base characters from the diacritical marks.
But that's not all. Having separated the base characters from the diacritical marks, you can then use String.prototype.replace() to remove diacritical marks from the string. This can be done by using a regular expression to match the diacritical marks in the given Unicode range (u0300 to u036f) and replacing them with empty strings.

Solution

const removeAccents = str =>
  str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');

removeAccents('Antoine de Saint-Exupéry'); // 'Antoine de Saint-Exupery'


But that's not all. Having separated the base characters from the diacritical marks, you can then use String.prototype.replace() to remove diacritical marks from the string. This can be done by using a regular expression to match the diacritical marks in the given Unicode range (u0300 to u036f) and replacing them with empty strings.

Code Snippets

const removeAccents = str =>
  str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');

removeAccents('Antoine de Saint-Exupéry'); // 'Antoine de Saint-Exupery'

Context

From 30-seconds-of-code: remove-accents

Revisions (0)

No revisions yet.