简体   繁体   中英

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://'); but I actually got url('https a //');

What am I missing? jsfiddle here . 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 .

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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