JavaScript
String Manipulation
Programming
Diacritics Removal
Text Processing

Remove accents and diacritics from a string in JavaScript

Codemia Knowledge Hub ·

Decompose the string with normalize("NFD"), then delete the combining marks with a regular expression: str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""). This is useful for building URL slugs, making search accent-insensitive, or keeping a dataset uniform.

Unicode normalization with normalize("NFD")

String normalization has been part of ECMAScript since ES2015:

javascript
let string = "Café à l'ancienne";
let normalizedString = string.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
console.log(normalizedString); // Outputs: Cafe a l'ancienne
  • normalize("NFD") splits each character into its base letter and its diacritical marks. NFD is Normalization Form Decomposition, so é becomes e plus a combining acute accent.
  • /[\u0300-\u036f]/g matches the combining diacritical marks from U+0300 to U+036F, and replace removes them.

Where normalization falls short

  • Ligatures such as œ and æ are not decomposed this way and need extra replacements.
  • Some characters have no straightforward decomposed form and are not handled.

A character map for ligatures and older environments

For environments without .normalize(), or for exceptions like ligatures and ß, map characters explicitly:

javascript
1function removeDiacritics(str) {
2    const diacriticsMap = {
3        'ö': 'o', 'Ö': 'O', 'ü': 'u', 'Ü': 'U', // Add more mappings as needed
4        'ä': 'a', 'Ä': 'A', 'ß': 'ss'
5    };
6    return str.replace(/[öÖüÜäÄß]/g, (match) => diacriticsMap[match]);
7}
8
9console.log(removeDiacritics("Fünf Fußgänger überschreiten die Straße")); // Outputs: Funf Fussganger uberschreiten die Strasse

replace() with a callback looks up each matched character in the map and returns its replacement. You must list every character you want to handle.

MethodDescriptionLimitations
String Normalization (normalize)Uses Unicode standard to decompose characters and remove diacriticsHigh dependency on client environment Unicode compatibility
MappingMaps each specific character to its non-diacritic counterpartRequires manual definition of all mappings, less dynamic

Normalization handles most cases and is supported in modern environments. Add a map on top when you need ligatures or must support environments without normalize().


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design