简体   繁体   English

如何通过JavaScript解码unicode HTML?

[英]How to decode unicode HTML by JavaScript?

How to use JavaScript to decode from: 如何使用JavaScript解码:

\u003cb\u003estring\u003c/b\u003e

to

<b>string</b>

(I searched in internet, there are some site with same question, such as: Javascript html decoding (我在网上搜索,有一些网站有相同的问题,如: Javascript html解码
or How to decode HTML entities 如何解码HTML实体
but it dont have same encode fomat ) 但它没有相同的编码fomat
Thank you very much! 非常感谢你!

This is a dup of How do I decode a string with escaped unicode? 这是一个重复的如何使用转义的unicode解码字符串? . One of the answers given there should work: 其中一个答案应该有效:

var x = '\\u003cb\\u003estring\\u003c/b\\u003e';
JSON.parse('"' + x + '"')

Output: 输出:

'<b>string</b>'
decodeURIComponent('\u003cb\u003estring\u003c/b\u003e');

//  "<b>string</b>"

Edit - I would delete the above answer if I could. 编辑 - 如果可以,我会删除上面的答案。

The original question is a bit ambiguous. 最初的问题有点含糊不清。

console.log('\ string\ '); will already yield <b>string</b> 已经产生了<b>string</b>

If the \\ characters are escaped, then a replacement method could be used to replace \\\\ with just \\ , thus allowing the proper Unicode escape sequence. 如果\\字符被转义,则可以使用替换方法将\\\\替换为\\ ,从而允许正确的Unicode转义序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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