Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  169] [ 1]  / answers: 1 / hits: 69792  / 12 Years ago, wed, july 25, 2012, 12:00:00

I am comparing strings and have to replace umlauts in JS, but it seems JS does not recognize the umlauts in the strings. The text comes from the database and in the browser the umlauts do show fine.



function replaceUmlauts(string)
{
value = string.toLowerCase();
value = value.replace(/ä/g, 'ae');
value = value.replace(/ö/g, 'oe');
value = value.replace(/ü/g, 'ue');
return value;
}


As search patterns I tried:




  • ä, ö, ü

  • /ä/, /ö/, /ü/

  • ä, ö, ü (well total despair ;-))



To be sure, that it is not a matter with the replace function I tried indexOf:



console.log(value.indexOf('ä'));


But the output with all patterns is: -1



So I guess it is some kind of a problem with encoding, but as I said on the page the umlauts do just look fine.



Any ideas? This seems so simple...



EDIT:
Even if I found my answer, the problem was not really solved at the root (the encoding).
This is my page encoding:



<meta content=text/html; charset=UTF-8 http-equiv=Content-Type>


The database has: utf8_general_ci



Seems totally alright to me.


More From » encoding

 Answers
7

Either ensure that your script's encoding is correctly specified (in <script> tag or in page's header/meta if it's embedded) or specify symbols with uNNNN syntax that will always unambiguously resolve to some specific Unicode codepoint.



For example:



str.replace(/u00e4/g, ae)


Will always replace ä with ae, no matter what encoding is set for your page/script, even if it is incorrect.



Here are the codes needed for Germanic languages:



// Ü, ü     u00dc, u00fc
// Ä, ä u00c4, u00e4
// Ö, ö u00d6, u00f6
// ß u00df

[#84049] Monday, July 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ericmaximilianz

Total Points: 252
Total Questions: 118
Total Answers: 87

Location: Virgin Islands (U.S.)
Member since Tue, Jul 7, 2020
4 Years ago
;