在javascript中解码十六进制字符串 [英] Decoding hex string in javascript

查看:191
本文介绍了在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天全站免登陆