string.replace(fromCharCode() , '') cannot replace characters

user1127017 picture user1127017 · Jun 21, 2012 · Viewed 10.9k times · Source

When I parse the XML, it contains abnormal hex characters. So I tried to replace it with empty space. But it doesn't work at all.

Original character : �

hex code : (253, 255)

code :

xmlData = String.replace(String.fromCharCode(253,255)," ");

retrun xmlData;

I'd like to remove "ýÿ" characters from description. Is there anyone who have a trouble with replacing hex character to empty space?

Based on the answers, I've modified the code as follows:

testData = String.fromCharCode(253,255);
xmlData = xmlData.replace(String.fromCharCode(253,255), " "); 
console.log(xmlData);

but it still shows '�' on the screen..

Do you know why this still happens?

Answer

Leonid picture Leonid · Jun 21, 2012

The character code is actually 255 * 256 + 253 = 65533, so you would get something like this:

xmlData = xmlData.replace(String.fromCharCode(65533)," ");

String String.fromCharCode(253,255) is of two characters.