Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode unicode HTML by JavaScript?

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!

like image 570
Thanh Nguyen Avatar asked Apr 10 '13 15:04

Thanh Nguyen


People also ask

Can I use Unicode in JavaScript?

Unicode in Javascript source codeIn Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.

How do I decode a string with escaped Unicode?

Use a decoder that interprets the '\u00c3' escapes as unicode code point U+00C3 (LATIN CAPITAL LETTER A WITH TILDE, 'Ã'). From the point of view of your code, it's nonsense, but this unicode code point has the right byte representation when again encoded with ISO-8859-1 / 'latin-1' , so...

How do you unescape in JavaScript?

The unescape() function computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. The escape sequences might be introduced by a function like escape . Usually, decodeURI or decodeURIComponent are preferred over unescape .


2 Answers

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>'
like image 196
Joe Hildebrand Avatar answered Oct 19 '22 19:10

Joe Hildebrand


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.

like image 34
Brad M Avatar answered Oct 19 '22 19:10

Brad M