Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding hex-containing escape sequences in JavaScript strings

Tags:

javascript

I have a string in JS in this format:

http\x3a\x2f\x2fwww.url.com

How can I get the decoded string out of this? I tried unescape(), string.decode but it doesn't decode this. If I display that encoded string in the browser it looks fine (http://www.url.com), but I want to manipulate this string before displaying it.

Thanks.

like image 957
siger Avatar asked Nov 17 '10 20:11

siger


People also ask

How do you escape a sequence in JavaScript?

Javascript uses '\' (backslash) in front as an escape character. To print quotes, using escape characters we have two options: For single quotes: \' (backslash followed by single quote) For double quotes: \” (backslash followed by double quotes)

What does escape do in JavaScript?

The escape() function computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Note: This function was used mostly for URL queries (the part of a URL following ? ) —not for escaping ordinary String literals, which use the format \xHH .

How do you escape a special character in node JS?

To use a special character as a regular one, prepend it with a backslash: \. . That's also called “escaping a character”.

Which of the following escape sequence is used in JavaScript to insert a horizontal tab in HTML page?

\t : horizontal tab (U+0009 CHARACTER TABULATION)


3 Answers

You could write your own replacement method:

String.prototype.decodeEscapeSequence = function() {
    return this.replace(/\\x([0-9A-Fa-f]{2})/g, function() {
        return String.fromCharCode(parseInt(arguments[1], 16));
    });
};
"http\\x3a\\x2f\\x2fwww.example.com".decodeEscapeSequence()
like image 45
Gumbo Avatar answered Sep 30 '22 15:09

Gumbo


If you already have:

var encodedString = "http\x3a\x2f\x2fwww.url.com";

Then decoding the string manually is unnecessary. The JavaScript interpreter would already be decoding the escape sequences for you, and in fact double-unescaping can cause your script to not work properly with some strings. If, in contrast, you have:

var encodedString = "http\\x3a\\x2f\\x2fwww.url.com";

Those backslashes would be considered escaped (therefore the hex escape sequences remain unencoded), so keep reading.

Easiest way in that case is to use the eval function, which runs its argument as JavaScript code and returns the result:

var decodedString = eval('"' + encodedString + '"');

This works because \x3a is a valid JavaScript string escape code. However, don't do it this way if the string does not come from your server; if so, you would be creating a new security weakness because eval can be used to execute arbitrary JavaScript code.

A better (but less concise) approach would be to use JavaScript's string replace method to create valid JSON, then use the browser's JSON parser to decode the resulting string:

var decodedString = JSON.parse('"' + encodedString.replace(/([^\\]|^)\\x/g, '$1\\u00') + '"');

// or using jQuery
var decodedString = $.parseJSON('"' + encodedString.replace(/([^\\]|^)\\x/g, '$1\\u00') + '"');
like image 57
PleaseStand Avatar answered Sep 30 '22 15:09

PleaseStand


There is nothing to decode here. \xNN is an escape character in JavaScript that denotes the character with code NN. An escape character is simply a way of specifying a string - when it is parsed, it is already "decoded", which is why it displays fine in the browser.

When you do:

var str = 'http\x3a\x2f\x2fwww.url.com';

it is internally stored as http://www.url.com. You can manipulate this directly.

like image 26
casablanca Avatar answered Sep 30 '22 15:09

casablanca