diff --git a/snippets/removeAccents.md b/snippets/removeAccents.md index 3766fe329..6b49ba389 100644 --- a/snippets/removeAccents.md +++ b/snippets/removeAccents.md @@ -5,25 +5,11 @@ tags: string,beginner Removes accents from strings. -- Uses a set of known accents for all vowels. -- Iterates through the characters, replacing the accentued ones for its unaccentuated form. +- Converts the string to a normalized Unicode format. +- The diacritical marks are represented by an Unicode range and are replaced by empty strings. ```js -const removeAccents = (string) => { - const accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖòóôõöÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü'; - const noAccents = 'AAAAAAaaaaaaOOOOOOoooooEEEEeeeeIIIIiiiiUUUUuuuu'; - - let splitted = string.split(''); - - splitted = splitted.map((char) => { - const pos = accents.indexOf(char); - return (pos >= 0 ? noAccents[pos] : char); - }); - - const newString = splitted.join(''); - - return newString; -} +const removeAccents = string => string.normalize("NFD").replace(/[\u0300-\u036f]/g, "") ``` ```js