本文翻译自:What is the difference between decodeURIComponent and decodeURI?
JavaScript函数decodeURIComponent
和decodeURI
什么区别?
#1楼
参考:https://stackoom.com/question/38Uj/decodeURIComponent和decodeURI有什么区别
#2楼
As I had the same question, but didn't find the answer here, I made some tests in order to figure out what the difference actually is. 由于我有同样的问题,但没有在这里找到答案,我做了一些测试,以弄清楚实际上是什么区别。 I did this, since I need the encoding for something, which is not URL/URI related. 我做了这个,因为我需要编码的东西,这不是URL / URI相关。
-
encodeURIComponent("A")
returns "A", it does not encode "A" to "%41"encodeURIComponent("A")
返回“A”,它不会将“A”编码为“%41” -
decodeURIComponent("%41")
returns "A".decodeURIComponent("%41")
返回“A”。 -
encodeURI("A")
returns "A", it does not encode "A" to "%41"encodeURI("A")
返回“A”,它不会将“A”编码为“%41” -
decodeURI("%41")
returns "A".decodeURI("%41")
返回“A”。
-That means both can decode alphanumeric characters, even though they did not encode them. - 这意味着它们都可以解码字母数字字符,即使它们没有对它们进行编码。 However... 然而...
-
encodeURIComponent("&")
returns "%26".encodeURIComponent("&")
返回“%26”。 -
decodeURIComponent("%26")
returns "&".decodeURIComponent("%26")
返回“&”。 -
encodeURI("&")
returns "&".encodeURI("&")
返回“&”。 -
decodeURI("%26")
returns "%26".decodeURI("%26")
返回“%26”。
Even though encodeURIComponent does not encode all characters, decodeURIComponent can decode any value between %00 and %7F. 即使encodeURIComponent不对所有字符进行编码,decodeURIComponent也可以解码%00和%7F之间的任何值。
Note: It appears that if you try to decode a value above %7F (unless it's a unicode value), then your script will fail with an "URI error". 注意:如果您尝试解码高于%7F的值(除非它是unicode值),那么您的脚本将失败并显示“URI错误”。
#3楼
encodeURIComponent Not Escaped: encodeURIComponent未转义:
A-Z a-z 0-9 - _ . ! ~ * ' ( )
encodeURI() Not Escaped: encodeURI()未转义:
A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
#4楼
To explain the difference between these two let me explain the difference between encodeURI
and encodeURIComponent
. 为了解释这两者之间的区别,让我解释一下encodeURI
和encodeURIComponent
之间的区别。
The main difference is that: 主要区别在于:
- The
encodeURI
function is intended for use on the full URI.encodeURI
函数适用于完整URI。 - The
encodeURIComponent
function is intended to be used on .. well .. URI components that is any part that lies between separators (; / ? : @ & = + $ , #).encodeURIComponent
函数旨在用于.. well .. URI组件,它是位于分隔符之间的任何部分(; /?:@&= + $,#)。
So, in encodeURIComponent
these separators are encoded also because they are regarded as text and not special characters. 因此,在encodeURIComponent
这些分隔符也被编码,因为它们被视为文本而不是特殊字符。
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. 现在回到解码函数之间的差异,每个函数解码由其相应的编码对应物生成的字符串,处理特殊字符的语义及其处理。
#5楼
js> s = "http://www.example.com/string with + and ? and & and spaces";
http://www.example.com/string with + and ? and & and spaces
js> encodeURI(s)
http://www.example.com/string%20with%20+%20and%20?%20and%20&%20and%20spaces
js> encodeURIComponent(s)
http%3A%2F%2Fwww.example.com%2Fstring%20with%20%2B%20and%20%3F%20and%20%26%20and%20spaces
Looks like encodeURI
produces a "safe" URI by encoding spaces and some other (eg nonprintable) characters, whereas encodeURIComponent
additionally encodes the colon and slash and plus characters, and is meant to be used in query strings. 看起来encodeURI
通过编码空格和其他一些(例如不可打印的)字符产生“安全”URI,而encodeURIComponent
另外编码冒号和斜杠以及加号字符,并且用于查询字符串。 The encoding of + and ? +和?的编码 and & is of particular importance here, as these are special chars in query strings. 和&在这里特别重要,因为它们是查询字符串中的特殊字符。
#6楼
decodeURIComponent将解码URI特殊标记,如&,?,#等,decodeURI不会。