How to Replace Umlaut characters in JavaScript


Borislav Hadzhiev

Last updated: Nov 19, 2021


Photo from Unsplash

Replace Umlaut characters in JavaScript #

Use the replace() method to replace umlaut characters in JavaScript. The first parameter the method takes is the character you want to replace, and the second - the replacement string. The method returns a new string where the matches are replaced.

function replaceUmlauts(str) { return str .replace(/\u00df/g, 'ss') .replace(/\u00e4/g, 'ae') .replace(/\u00f6/g, 'oe') .replace(/\u00fc/g, 'ue') .replace(/\u00c4/g, 'Ae') .replace(/\u00d6/g, 'Oe') .replace(/\u00dc/g, 'Ue'); } // 👇️ "aehnlich Aepfel Muell oeffnen Oe Ue ss" console.log(replaceUmlauts('ähnlich Äpfel Müll öffnen Ö Ü ß'));

We created a reusable function which takes a string and replaces the umlaut characters with their equivalents.

We passed the following 2 parameters to the String.replace method:

  1. a regular expression to match in the string
  2. the replacement for each match

The forward slashes / / mark the beginning and end of the regular expression.

To make sure the encoding is correct, we used the unicode character points for the umlauts.

We used the g (global) flag because we want to replace all occurrences of the umlaut in the string, and not just the first occurrence.

The replace() method returns a new string, on which we can chain the next call to the method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.