Search code examples
javascriptregexmutated

Javascript regex replace has to keep german mutated vowels


In a search field there should only be allowed regular characters and german mutated vowels like ä, ö, ü, Ä, Ö, Ü and ß.

My regex looks like:

/(<([^>]+)>)|[^a-zA-Z0-9äöüÄÖÜß\s]/ig

The replace:

phrase.replace(regex, "")

Before the replace:

Ärzte

After the replace:

rzte

Unfortunately the mutated vowels are getting removed by the replace. Any suggestions to keep these characters are appreciated.

Thanks in advance.


Solution

  • The issue is most likely the charset not being set to UTF-8. You should fix your charset, but better practice might be to use hex codes for that regex instead of using the character directly, and then leaving some comments in there so you remember what the hex codes were for.

    Check if this works

    phrase.replace(/(<([^>]+)>)|[^a-z0-9\xE4\xF6\xFC\xC4\xD6\xDC\xDF\s]/ig, "\n")
    

    You can find some other hex escapes from here http://www.javascripter.net/faq/accentedcharacters.htm