如何解码JavaScript中的字符串? [英] How to decode a string in JavaScript?
问题描述
这不适合我:
var foo = "Collection%3A 9 Bad Interviews With Former GOP Presidential Candidates";
console.log(decodeURI(foo));
输出:
Collection%3A 9 Bad Interviews With Former GOP Presidential Candidates
这不正确,如果你在这样的网站上输入foo字符串:
That's not correct, if you enter the foo string on a site like this:
http://meyerweb.com/eric/tools/dencoder/
它显示正确的输出,即:
It shows the right output, which is:
Collection: 9 Bad Interviews With Former GOP Presidential Candidates
如何正确解码字符串?
推荐答案
decodeURI 和 decodeURIComponent
主要区别在于:
The main differences are:
因此,在 encodeURIComponent 中这些分隔符也被编码,因为它们被视为文本而不是特殊字符。
So, in encodeURIComponent these separators are encoded also because they are regarded as text and not special characters.
现在回到解码函数之间的差异,每个函数解码由其对应的编码对应生成的字符串,处理特殊字符的语义及其处理。
Now back to the difference between the decode functions, each function decodes strings generated by its corresponding encode counterpart taking care of the semantics of the special characters and their handling.
所以在你的情况下 decodeURIComponent 这份工作
so in your case decodeURIComponent does the job
这篇关于如何解码JavaScript中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!