If you already have:
var encodedString = "http\x3a\x2f\x2fwww.url.com";
Then , manually decoding the string is not required . The JavaScript interpreter will already decode the escape sequences for you, and in fact double unescaping can cause your script to not work properly with some lines. If, on the contrary, you have:
var encodedString = "http\\x3a\\x2f\\x2fwww.url.com";
These backslashes are considered escaped (so hexadecimal escape sequences remain unencoded), so keep reading.
The easiest way in this 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 valid JavaScript escape code. However, do not do this if the line does not come from your server ; if so, you will create a new security vulnerability because eval can be used to execute arbitrary JavaScript code.
A better (but less concise) approach would be to use the JavaScript string replacement method to create valid JSON, and then use the browser 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') + '"');
PleaseStand
source share