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!
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.
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...
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 .
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>'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With