Home > Web Front-end > JS Tutorial > How Can I Remove Accents and Diacritics from Strings in JavaScript?

How Can I Remove Accents and Diacritics from Strings in JavaScript?

Mary-Kate Olsen
Release: 2024-12-25 19:43:09
Original
969 people have browsed it

How Can I Remove Accents and Diacritics from Strings in JavaScript?

Remove Accents and Diacritics from Strings in JavaScript

Removing accentuated characters from strings is essential for tasks like sorting and search operations. There are various approaches to achieve this in JavaScript.

Using Regular Expressions

One common method involves using regular expressions to replace accented characters with their non-accented equivalents. However, in older browsers like IE6, regular expressions may encounter issues.

Leveraging String.prototype.normalize()

ES2015 introduced the String.prototype.normalize() method, which allows for the normalization of Unicode strings. By normalizing to the "NFD" form, you can decompose combined graphemes into simpler ones. This allows you to easily remove diacritics by filtering out specific Unicode characters.

const str = "Crème Brûlée";
str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); // "Creme Brulee"
Copy after login

Alternatively, you can use Unicode property escapes:

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

Sorting Strings with Accents and Diacritics

For sorting purposes, you can use the Intl.Collator object to handle accent and diacritic normalization.

const c = new Intl.Collator();
[...names].sort(c.compare); // Sorts names without considering accents or diacritics
Copy after login

Additionally, you can use String.prototype.localeCompare(), which considers accents and diacritics by default.

[...names].sort((a, b) => a.localeCompare(b));
Copy after login

In summary, using String.prototype.normalize() or Intl.Collator provides a robust approach for removing accents and diacritics from strings or sorting strings without being impacted by accent variations.

The above is the detailed content of How Can I Remove Accents and Diacritics from Strings in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template