How to decode unicode HTML by JavaScript?

痴心易碎 提交于 2019-12-03 13:31:28

问题


How to use JavaScript to decode from:

\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
or How to decode HTML entities
but it dont have same encode fomat)
Thank you very much!


回答1:


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('\u003cb\u003estring\u003c/b\u003e'); will already yield <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.




回答2:


This is a dup of How do I decode a string with escaped unicode?. One of the answers given there should work:

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

Output:

'<b>string</b>'


来源:https://stackoverflow.com/questions/15929686/how-to-decode-unicode-html-by-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!