简体   繁体   中英

How to convert this text to correct HTML characters using Javascript

How to convert this text to correct HTML characters using Javascript:

'PingAsyncTask - Token v\�lido'

Put in your console:

console.log('PingAsyncTask - Token v\�lido');

I already try all common functions:

  1. https://gist.github.com/chrisveness/bcb00eb717e6382c5608
  2. http://monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
  3. http://jsfromhell.com/geral/utf-8

Can anyone help me?

If your document is already UTF-8 you don't need to do anything special. The string is already encoded correctly in JavaScript, so when you write it into the document it'll show up correctly. You can see it in this fiddle: https://jsfiddle.net/baar4ew8/

PS The character in your code ( \� ) is U+FFFD , the Unicode replacement character. Most fonts render it as a black diamond with a question mark inside, or just an empty box. Here's how Stack Overflow renders it:

If you're seeing that in your output, your string is being rendered correctly.

If you think you should be seeing some other character, then your problem isn't in the HTML or JavaScript—it's with the source of your data, whatever that might be. When a program converts text from a non-Unicode encoding to a Unicode encoding like UTF-8, characters that don't exist in Unicode are replaced with U+FFFD ( )—hence "replacement character." If you're expecting some character that does exist in Unicode but you're getting U+FFFD then it might be the case that the program converting the text to UTF-8 doesn't know what encoding it was originally in and so converted it incorrectly. For example, if you stored text with encoding X in a database table with encoding Y without first converting it to encoding Y.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM