我不确定这叫什么,所以我很难找到它。如何使用JavaScript将Unicode字符串从http\u00253A\u00252F\u00252Fexample.com
解码为http://example.com
?我尝试了unescape
,decodeURI
和decodeURIComponent
,所以我想剩下的唯一事情就是字符串替换。
编辑:未键入字符串,而是来自另一段代码的子字符串。因此,要解决此问题,您必须先从以下内容开始:
var s = 'http\\u00253A\\u00252F\\u00252Fexample.com';
我希望这表明为什么unescape()不起作用。
最佳答案
编辑(2017-10-12):
@MechaLynx和@ Kevin-Weber注意到unescape()
已从非浏览器环境中弃用,并且在TypeScript中不存在。 decodeURIComponent
是一个替代品。为了获得更大的兼容性,请改用以下内容:
decodeURIComponent(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
原始答案:
unescape(JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"'));
> 'http://example.com'
您可以将所有工作卸载到
JSON.parse