简体   繁体   English

如何在javascript中使用unicode和utf-8解码字符串?

[英]How to decode a string with unicode and utf-8 in javascript?

Ok please help - I am having trouble unescaping all the unicode/utf-8 in my javascript string. 好的,请帮助-我在转义JavaScript字符串中的所有unicode / utf-8时遇到麻烦。 I looked at this question but I don't understand how to form a regex that covers everything I need. 我看着这个问题,但我不明白如何形成一个涵盖我所需的一切的正则表达式。 For example, a string I have might be: "\\xe2\\x80\\x98my\, union" and I would want an output of 'my, union. 例如,我拥有的字符串可能是:“ \\ xe2 \\ x80 \\ x98my \\ u002c union”,而我想要的输出是'my,union。 I am very very confused about how to deal with this format, I tried looking at this resource but I just don't understand how to use or construct a regex. 我对如何处理这种格式感到非常困惑,我尝试查看此资源,但我只是不了解如何使用或构造正则表达式。 Also, if there is another much easier way to unescape these characters, ideas would be welcome, I am a total beginner at this. 另外,如果有另一种更容易的方式来对这些字符进行转义,那么欢迎提出想法,我是这方面的初学者。 I did try unescape(JSON.parse(mystring)) but this did not work either. 我确实尝试过unescape(JSON.parse(mystring))但这也不起作用。 Please help!! 请帮忙!!

您可以使用decodeURIComponentescape ,在详细介绍本博文

decodeURIComponent(escape('\xe2\x80\x98my\u002c union')) // "‘my, union"

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

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