解码 JavaScript 字符串中包含十六进制的转义序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4209104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:22:50  来源:igfitidea点击:

Decoding hex-containing escape sequences in JavaScript strings

javascript

提问by siger

I have a string in JS in this format:

我在 JS 中有一个这种格式的字符串:

http\x3a\x2f\x2fwww.url.com

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.

我怎样才能从中得到解码的字符串?我试过 unescape(), string.decode 但它没有解码这个。如果我在浏览器中显示该编码字符串,它看起来不错 (http://www.url.com),但我想在显示之前操作该字符串。

Thanks.

谢谢。

回答by Gumbo

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()

回答by casablanca

There is nothing to decode here. \xNNis 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.

这里没有什么可解码的。\xNN是 JavaScript 中的转义字符,表示代码为 NN 的字符。转义字符只是指定字符串的一种方式 - 当它被解析时,它已经被“解码”,这就是它在浏览器中显示良好的原因。

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.

它在内部存储为http://www.url.com. 您可以直接操作它。

回答by gblazex

You don't need to decode it. You can manipulate it safely as it is:

你不需要解码它。您可以按原样安全地操作它:

var str = "http\x3a\x2f\x2fwww.url.com";
?alert(str.charAt(4));  // :
alert("\x3a" === ":"); // true
alert(str.slice(0,7))?; // http://

回答by PleaseStand

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:

然后手动解码字符串是不必要的。JavaScript 解释器已经为您解码转义序列,事实上,双重转义会导致您的脚本无法正确处理某些字符串。相反,如果您有:

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 evalfunction, which runs its argument as JavaScript code and returns the result:

在这种情况下,最简单的方法是使用该eval函数,该函数将其参数作为 JavaScript 代码运行并返回结果:

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

This works because \x3ais 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 evalcan be used to execute arbitrary JavaScript code.

\x3a是有效的,因为它是一个有效的 JavaScript 字符串转义码。但是,如果字符串不是来自您的服务器,请不要这样做;如果是这样,您将创建一个新的安全漏洞,因为eval可用于执行任意 JavaScript 代码。

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 parserto decode the resulting string:

更好(但不那么简洁)的方法是使用 JavaScript 的字符串替换方法来创建有效的 JSON,然后使用浏览器的 JSON 解析器对结果字符串进行解码:

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

// or using jQuery
var decodedString = $.parseJSON('"' + encodedString.replace(/([^\]|^)\x/g, '\u00') + '"');

回答by Raphael Müller

maybe this helps: http://cass-hacks.com/articles/code/js_url_encode_decode/

也许这有帮助:http: //cass-hacks.com/articles/code/js_url_encode_decode/

function URLDecode (encodedString) {
var output = encodedString;
var binVal, thisString;
var myregexp = /(%[^%]{2})/;
while ((match = myregexp.exec(output)) != null
           && match.length > 1
         && match[1] != '') {
binVal = parseInt(match[1].substr(1),16);
thisString = String.fromCharCode(binVal);
output = output.replace(match[1], thisString);
}
return output;
}

回答by Илья Зеленько

2019

2019年

You can use decodeURIor decodeURIComponentand not unescape.

您可以使用decodeURIdecodeURIComponent而不是unescape

console.log(
  decodeURI('http\x3a\x2f\x2fwww.url.com')
)

回答by ROOT

For modern Javascript implementation, its better to use one of the following function:

对于现代 Javascript 实现,最好使用以下函数之一:

  • decodeURIComponent: and its intedend to be used on parts of the URI, but still can be used to decode full URI.
  • decodeURI: use to decode full URI.

Working snippet for both:

两者的工作片段:

const uriEncoded = 'http\x3a\x2f\x2fwww.url.com'
console.log('decodeURIComponent: ', decodeURIComponent(uriEncoded));
console.log('decodeURI: ', decodeURI(uriEncoded));