How to unescape UTF-8 characters in Node (\u00f6)?

后端 未结 1 1539
[愿得一人]
[愿得一人] 2021-01-19 17:21

I have a properties file which is encoded using ISO Latin but with special characters as UTF-8 escape sequences, for example the following string:

Einstellun         


        
相关标签:
1条回答
  • 2021-01-19 17:53

    You either have to parse it as a string-literal, so the unicode-codes are parsed by the engine, therefore you have to wrap it in quotes before running it through JSON.parse().

    JSON.parse('"' + str + '"');
    //if you use " in your string, you would have to escape it
    JSON.parse('"' + str.split('"').join('\\"') + '"');
    

    or you search for the unicode-codes and replace them on your own

    str.replace(/\\u([0-9a-fA-F]{4})/g, (m,cc)=>String.fromCharCode("0x"+cc));
    
    0 讨论(0)
提交回复
热议问题