Reputation: 20438
I have retrieved a string from the database which appears to have invalid characters.
I've tried to save a JSFiddle, but JS Fiddle appears to clean up the code.
Therefore, I'll paste a screenshot and hope that it is enough.
Here is the code in my IDE
If I paste that code into JSFiddle, I get:
In both cases, after 'Headings', there is a strange illegal character which is highlighted.
How do I purge these invalid characters from my string?
Upvotes: 0
Views: 263
Reputation: 1086
It should be a encoding issue that happens when you save your data in the DB.The encoding of the data that you are receiving should be same as the encoding of database where you are storing it. set the charset to utf8. also please check if you have added this meta tag <meta charset="utf-8">
If you use php with mysql this would be the way to do it.
mysqli_set_charset($con,"utf8");
Upvotes: 0
Reputation: 2740
It's better if you can clean the string before getting added to the DB if you think it's unnecessary.
Otherwise,
Invalid characters get converted to 0xFFFD
on parsing, so you can replace it like,
myString = myString.replace(/\uFFFD/g, '')
Or
You could use a white list. If you want to only accept letters, numbers, space, and a few punctuation characters, you could do,
myString = myString.replace(/[^a-z0-9 ,.?!]/ig, '')
Upvotes: 2