简体   繁体   中英

Javascript convert Var to UTF8 string

I've hit a strange situation that I just can't seem to figure my way out of. I have a string that contains UTF8 characters (escaped). I've tried the decodeURIComponent(escape(str)) along with a bunch of other suggested fixes, as yet without success.

I've written this function to take the string, find the escaped characters, and replace them with straight UTF8.

var unescapeUTF8 = function(str) {
    var matches = str.match(/\\u.{4}/g);
    if (matches == null) return str;
    for (var item of matches)
    {
        // testing
        console.log(new String(item));
    }
    ....
    ....
    ....
};

From testing, I know that if I go new String("\ģ") I will get back a string object String {0: "ģ", length: 1, [[PrimitiveValue]]: "ģ"}

It seems no matter what I do to the string in the function above, I can not get it to convert from it's escaped to ģ

I've managed to 'create' the issue in my browser by opening developer tools and running the following

var x = "\\u0123";
console.log(x); // == "\u0123"
new String(x); // == String {0: "\", 1: "u", 2: "1", 3: "3", 4: "2", 5: "4", length: 6, [[PrimitiveValue]]: "\u1324"}

Can anyone figure out how to convert "x" into a UTF8 character please...

Since those escape sequences are, at first blush, valid JSON escape sequences, the easiest method is to parse the string as a JSON string:

 var x = "\\\ģ"; console.log(JSON.parse('"' + x + '"')); 

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