Remove accents from a string in JavaScript
function toNormalForm(str) {
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}
console.log(toNormalForm('Text with ä â ë ü í ő ń')); // Text with a a e u i o n
Sometimes you need to convert a string to its normalized version that doesn't contain any special letters from languages different from English. French, German, Spanish, Hungarian languages have some special characters (letters with accents) like ä â ë ü í ő ń
. To remove all accents in a string using vanilla JavaScript use the normalize
function supplemented by a string replace
.
The normalize()
method returns the Unicode Normalization Form of the string. Special characters can be represented in two ways. For example, the character "ñ" for example can be represented by either of:
- The single code point
U+00F1
. - The code point for "n"
U+006E
is followed by the code point for the combining tildeU+0303
.
So the solution is to convert all letters with accents to the 2 elements version using the normalize
method and then remove all special (accent) element using the replace
method.
- Remove double quotes from a string in JavaScript
- Check if date is valid in JavaScript
- Check if string contains a substring in JavaScript
- Check if a string is empty in JavaScript
- Escape quotes in JavaScript
- Count the number of characters in a string with JavaScript
- Concatenate strings in JavaScript
- Check if string ends with slash using JavaScript
- Get the last character of a string in JavaScript
- Convert array to comma separated string using JavaScript
- Check if string contains a specific character using JavaScript
- Remove spaces from string at the beginning, end, or both in JavaScript
- Remove all extra spaces from a string in JavaScript
- Remove emojis from a string with JavaScript
- Check if a string ends with number in JavaScript
- Remove specific character from a string in JavaScript
- Remove protocol from URL with JavaScript
- Remove the last character from a string in JavaScript
- Count the number of words in a string using JavaScript
- Count the number of lines in a string in JavaScript
- Convert string to int in JavaScript
- Convert string to lowercase with JavaScript
- Generate UUID in JavaScript
- Generate random color with JavaScript
- Convert string to kebab case in JavaScript
- Convert snake case to camel case in JavaScript
- Capitalize words in a string using JavaScript
- Generate random string in JavaScript
- Convert string to character array in JavaScript
- Convert camel case to snake case in JavaScript
- Remove accents from a string in JavaScript
- Convert a string to sentence case in JavaScript
- Convert string to title case in JavaScript
- Convert string to snake case in JavaScript
- Convert string to pascal case in JavaScript
- Convert string to camel case in JavaScript
- Convert string to uppercase with JavaScript
- Convert object to JSON string in JavaScript
- Parse JSON string in JavaScript
- convert number to string in JavaScript
- Use colors in JavaScript console
- Get string length in JavaScript
- Split a string into words
- Replace line break with br in JavaScript
- Replace spaces with dashes in JavaScript
- Create multiline string in JavaScript