如何解码JavaScript中的字符串? [英] How to decode a string in JavaScript?

查看:69
本文介绍了如何解码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

主要区别在于:

  • encodeURI 旨在用于完整的URI。

  • encodeURIComponent 旨在用于.. well .. URI组件,它是位于分隔符之间的任何部分(; /? :@& = + $,#)。

    The main differences are:

  • encodeURI is intended to be used on the full URI.
  • encodeURIComponent is intended to be used on .. well .. URI components that is any part that lies between separators (; / ? : @ & = + $ , #).

    因此,在 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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆