Edit (2017-10-12):
@MechaLynx and @Kevin-Weber note that unescape() is deprecated from non-browser environments and does not exist in TypeScript. decodeURIComponent is a drop-in replacement. For broader compatibility, use the below instead:
decodeURIComponent(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
Original answer:
unescape(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
You can offload all the work to JSON.parse
Edit (2017-10-12):
@MechaLynx and @Kevin-Weber note that unescape() is deprecated from non-browser environments and does not exist in TypeScript. decodeURIComponent is a drop-in replacement. For broader compatibility, use the below instead:
decodeURIComponent(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
Original answer:
unescape(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
You can offload all the work to JSON.parse
UPDATE: Please note that this is a solution that should apply to older browsers or non-browser platforms, and is kept alive for instructional purposes. Please refer to @radicand 's answer for a more up to date answer.
This is a unicode, escaped string. First the string was escaped, then encoded with unicode. To convert back to normal:
var x = "http\\u00253A\\u00252F\\u00252Fexample.com";
var r = /\\u([\d\w]{4})/gi;
x = x.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp, 16)); } );
console.log(x); // http%3A%2F%2Fexample.com
x = unescape(x);
console.log(x); // http://example.com
To explain: I use a regular expression to look for \u0025. However, since I need only a part of this string for my replace operation, I use parentheses to isolate the part I'm going to reuse, 0025. This isolated part is called a group.
The gi part at the end of the expression denotes it should match all instances in the string, not just the first one, and that the matching should be case insensitive. This might look unnecessary given the example, but it adds versatility.
Now, to convert from one string to the next, I need to execute some steps on each group of each match, and I can't do that by simply transforming the string. Helpfully, the String.replace operation can accept a function, which will be executed for each match. The return of that function will replace the match itself in the string.
I use the second parameter this function accepts, which is the group I need to use, and transform it to the equivalent utf-8 sequence, then use the built - in unescape function to decode the string to its proper form.
How to decode unicode HTML by JavaScript? - Stack Overflow
Convert Unicode characters
UTF8 encoding vs base64 encoding of an image
innerHTML and newlines
I suspect there's a slight confusion here.
In Javascript (and most common languages), \n inside a string literal isn't placing \ and n in the string at all; \n inside a string literal is converted when your code is parsed, and understood as an actual newline; the string in memory contains an actual newline character. HTML doesn't do this, so the \n inside your HTML code is simply understood as \ and n. When your JS reads a string from any source that isn't a string literal (quoted text inside JS code) it is not parsed, it does not convert escape codes, it takes the string as is.
In general, it doesn't make much sense to convert \n to NL in strings from other sources because those sources can generally include NL characters directly. There are, of course, potential situations where, for some reason, the source can't contain newlines, but as it stands your fiddle isn't one of them.
\u0027s: HTML has its own escaping technique (entity codes) to encode unicode characters. In HTML's case, they are parsed and converted as they are rendered to screen; element.innerHTML will return a string containing these codes in their original form. If all you're going to do with these strings in the end is place them back into the HTML that's totally fine, as they will still be decoded before they are shown to the user. But if, for example, you intend to perform a per-character operation against the string, you probably want them to contain the proper unicode characters. There are ways to read decoded text from elements if that's really what you need. Ultimately, an HTML document should contain HTML encoded text.
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.