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("\u0123")
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 \u0123
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...