解码JavaScript字符串中包含十六进制的转义序列 [英] Decoding hex-containing escape sequences in JavaScript strings

查看:39
本文介绍了解码JavaScript字符串中包含十六进制的转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JS中有以下格式的字符串:

I have a string in JS in this format:

http \ x3a \ x2f \ x2fwww.url.com

http\x3a\x2f\x2fwww.url.com

如何从中获取解码后的字符串?我尝试了unescape(),string.decode,但它没有对此进行解码.如果我在浏览器中显示该编码的字符串,看起来就很好(http://www.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.

谢谢.

推荐答案

您可以编写自己的替换方法:

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

这篇关于解码JavaScript字符串中包含十六进制的转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆