Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 852
user1127017 Avatar asked Jun 21 '12 09:06

user1127017


People also ask

How to replace characters in a string in js?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced.

What does string fromCharCode do?

The String. fromCharCode() method converts Unicode values to characters. The String. fromCharCode() is a static method of the String object.

How to replace text using JavaScript?

The JavaScript replace() method is used to replace any occurrence of a character in a string or the entire string. It searches for a string corresponding to either a particular value or regular expression and returns a new string with the modified values.


1 Answers

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.

like image 59
Leonid Avatar answered Sep 19 '22 10:09

Leonid