JavaScript Snippet

Strip Accents and Diacritics

Difficulty: Medium

Removing accents from a string is the secret behind diacritic-insensitive search, slug normalization, and sort-key generation. The canonical answer is `normalize('NFD') + replace combining marks`, which works for Latin scripts, but locale-aware comparison and case-folding need richer tools. This snippet starts with the one-liner, adds a case-folded variant for matching, and ends with `Intl.Collator` for true locale-correct comparison.

Code Snippets
/

Strip Accents and Diacritics

Strip Accents and Diacritics

Removing accents from a string is the secret behind diacritic-insensitive search, slug normalization, and sort-key generation. The canonical answer is `normalize('NFD') + replace combining marks`, which works for Latin scripts, but locale-aware comparison and case-folding need richer tools. This snippet starts with the one-liner, adds a case-folded variant for matching, and ends with `Intl.Collator` for true locale-correct comparison.

JavaScript
Medium
3 snippets
strings
regex
js-web-apis

858 views

4

Unicode normalization form D (NFD) decomposes a single accented code point like 'é' (U+00E9) into a base letter 'e' (U+0065) followed by a combining acute accent (U+0301). Once the accent is its own code point, a regex strip of the combining-marks block (U+0300 through U+036F) removes it, leaving the base letter intact. This is the ASCII-folding trick used by every search engine and slug generator: cheap (one allocation), correct for Latin and Greek, and fully built into the language since ES2015. It does NOT handle scripts that have a single code point per character with no decomposition, like Arabic, Hebrew, or CJK, where the input passes through unchanged.