简体   繁体   English

在 Javascript 中替换字符串中的十六进制转义序列的问题

[英]Issue with replacing hexadecimal escape sequences in a string in Javascript

I'm trying to fix a string which has some encoding characters in it.我正在尝试修复其中包含一些编码字符的字符串。

I thought I should be able to match the hex characters of the special characters and convert them back to a normal character.我想我应该能够匹配特殊字符的十六进制字符并将它们转换回普通字符。

Here is my example code:这是我的示例代码:

let str = "url('https\3a //');";
str = str.replace(/\x5C\x33\x61\x20/g,":"); // equivalent to '\3a '
console.log(str);  

I expected the output to be url('https://');我希望 output 是url('https://'); but I actually got url('https a //');但我实际上得到了url('https a //');

What am I missing?我错过了什么? jsfiddle here . jsfiddle在这里 Is this some sort of multi byte character issue?这是某种多字节字符问题吗? I looked at the resulting string in a hex editor and the replaced characters seem to be \x03\x61\x20 rather than the expected \x3A .我在十六进制编辑器中查看了生成的字符串,替换的字符似乎是\x03\x61\x20而不是预期的\x3A

EDIT: why has this been down voted?编辑:为什么这被否决了? It is a fair question isn't it?这是一个公平的问题,不是吗?

Is the code that you use really needs to be in this form?您使用的代码真的需要采用这种形式吗? I got the desired result using "3a".我使用“3a”得到了想要的结果。

 str = "url('https\3a //');"; str = str.replace(/\3a /g,":"); // equivalent to '\3a ' console.log(str); //result: url('https://');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM