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

Answer from radicand on Stack Overflow
🌐
Freecodeformat
freecodeformat.com › unicode-js.php
Online Unicode JS Encoder/Decoder - Javascript Unicode Escape
Convert between characters and Javascript Unicode escape sequences (\uHHHH) online. Supports drag-and-drop, ES6, and fast load times.
Top answer
1 of 7
157

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

2 of 7
132

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.

Discussions

How to decode unicode HTML by JavaScript? - Stack Overflow
How to use JavaScript to decode from: \u003cb\u003estring\u003c/b\u003e to string (I searched in internet, there are some site with same question, such as: Javascript html deco... More on stackoverflow.com
🌐 stackoverflow.com
Convert Unicode characters
It appears that sometimes I get unicode characters like ’ for " ’ ". Is there a function or a way I could parse my whole quote to remove enventual unicode characters · Its caused by JSX double escaping the string. It’s escaped anyway as a security precaution, but what you have from ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
3
0
February 28, 2019
UTF8 encoding vs base64 encoding of an image
Base64 is used to encode data into text format, so any character will be easy to send as text or print. Each resulting character will be a number or Latin letter (lower or uppercase). Meaning no special characters nor quotes nor even spaces. This encoding is used for example in basic authentication, as it's easy to send header value when no special characters used. UTF-8 is totally other beast. It's used to represent characters from different languages. Straight back to your question: So I have encoded an image file with base64 and I can see readable text, but If i use utf-8 I have unreadable/weird characters. Can someone explain why this is the case ? That's because Base64 possible characters are all simple numbers and Latin letters. While UTF-8 has a table of valid codes, you cannot just get random bytes and expect it to be valid UTF-8. More on reddit.com
🌐 r/learnjavascript
3
3
February 1, 2022
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.

More on reddit.com
🌐 r/javascript
4
5
April 11, 2014
🌐
Magic Tool
magictool.ai › tool › unicode-decoder-encoder
Unicode Decoder & Encoder
Decode or Encode text characters into unicode entities and vice-versa using javascript unicode escape/unescape functions.
🌐
GitHub
github.com › sindresorhus › unicode-escapes
GitHub - sindresorhus/unicode-escapes: Encode and decode Unicode escapes in a string · GitHub
August 25, 2023 - Can be useful when a tool or service returns text with encoded Unicode characters. ... import {encodeUnicodeEscapes, decodeUnicodeEscapes} from 'unicode-escapes'; console.log(encodeUnicodeEscapes('Hello, โลก')); //=> 'Hello, \u{e42}\u{e25}\u{e01}' console.log(decodeUnicodeEscapes('Hello, \\u{e42}\\u{e25}\\u{e01}')); //=> 'Hello, โลก'
Author   sindresorhus
🌐
Base64
base64.sh › home › all tools › js unicode
JavaScript Unicode Escape | \uXXXX Encoder Decoder
Free online JavaScript Unicode escape encoder and decoder. Convert text to \uXXXX format. Support for ES6 \u{} syntax and astral plane characters.
🌐
Arayofsunshine
arayofsunshine.dev › unicode
Unicode encode/Unicode decode/Emoji encode/Emoji decode - Text to Hex/CSS/JS Online
Convert text and emoji to Unicode code points, JavaScript escape sequences, and CSS content codes. A developer-friendly tool for character encoding.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › decodeURI
decodeURI() - JavaScript - MDN Web Docs
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015 · The decodeURI() function decodes a Uniform Resource Identifier (URI) previously created by encodeURI() or a similar routine · A new string representing ...
🌐
Teleport
goteleport.com › home › resources › tools › unicode escape/unescape | encode/decode special characters
Unicode Escape/Unescape | Encode/Decode Special Characters | Teleport
To convert a string containing Unicode escape sequences back to their corresponding characters, use the decode() method with the unicode_escape encoding:
🌐
Internetwache
encoder.internetwache.org
Multi-Encoder
Base64 · Binary (Ascii) · Binary (Integer) · Hex (Ascii) · Hex (Integer) · Hex (JS) · Oct (Ascii) · Oct (Integer) · Oct (JS) · Unicode
🌐
DevKitLab
devkitlab.com › encode › unicode converter
Unicode Converter | Escapes and Code Points | DevKitLab
Convert text to Unicode escapes and decode them back in the browser, with JavaScript \u, U+ code points, HTML entities, and legacy %u output.
🌐
javaspring
javaspring.net › blog › how-to-decode-unicode-html-by-javascript
How to Decode Unicode HTML in JavaScript: Convert \uXXXX Escape Sequences to Plain Strings — javaspring.net
Use String.fromCodePoint() instead, which supports all Unicode code points. Use /\\u\{([0-9a-fA-F]{1,6})\}/g to match ES6+ \u{XXXXX} sequences: ... String.fromCodePoint(parseInt(hex, 16)) handles code points > U+FFFF. const escapedEmoji = 'Smile: ...
🌐
Mother Eff
mothereff.in › utf-8
UTF-8 encoder/decoder
This tool uses utf8.js to UTF-8-encode any string you enter in the ‘decoded’ field, or to decode any UTF-8-encoded string you enter in the ‘encoded’ field.
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › unicode-encoding--javascript-in-browser
Unicode Encoding : JavaScript in Browser | Encoding Solutions Across Programming Languages
Learn how to use Unicode encoding in JavaScript for browsers, enabling seamless text representation and enhancing web compatibility.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › fromCharCode
JavaScript String fromCharCode() - Convert To Character | Vultr Docs
September 27, 2024 - The String.fromCharCode() method in JavaScript simplifies the conversion of Unicode values to characters, enhancing control over text manipulation and encoding in your web applications.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › unescape
unescape() - JavaScript - MDN Web Docs
Note: unescape() is a non-standard function implemented by browsers and was only standardized for cross-engine compatibility. It is not required to be implemented by all JavaScript engines and may not work everywhere. Use decodeURIComponent() or decodeURI() if possible.
🌐
SSOJet
ssojet.com › character-encoding-decoding › unicode-in-javascript-in-browser
Unicode in JavaScript in Browser | Encoding Standards for Programming Languages
Handling international characters ... browser. This guide dives into Unicode, explaining how JavaScript natively supports it and how to correctly work with characters beyond the basic ASCII set. You'll learn how to properly encode, decode, and manipulate strings containing ...
🌐
Delicious-insights
delicious-insights.com › en › posts › js-strings-unicode
Strings and Unicode in JavaScript • Delicious Insights
May 8, 2020 - JavaScript strings are encoded using UTF-16 (or UCS-2, depending on the implementation; it’s a distinction without much of a difference). Every string position therefore refers to 16 bits of data, or 2 bytes. This is indeed enough to encode most Unicode codepoints in the U+0000 to U+FFFF range, but not beyond (despite there being a truckload beyond, in practice adding up to around 144,000 glyphs).
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Convert Unicode characters - JavaScript - The freeCodeCamp Forum
February 28, 2019 - It appears that sometimes I get unicode characters like ’ for " ’ ". Is there a function or a way I could parse my whole quote to remove enventual unicode characters · Its caused by JSX double escaping the string. It’s escaped anyway as a security precaution, but what you have from ...