How to Replace Umlaut characters in JavaScript

avatar

Borislav Hadzhiev

Fri Nov 19 20211 min read

banner

Photo by David Marcu

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.

index.js
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.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee