43

在 JavaScript 中,这些有什么区别?

  1. escape()/unescape()
  2. encodeuri()/decodeuri()
  3. encodeURIComponent()/decodeURIComponent()
4

5 回答 5

66

对于有视觉头脑的人,这里有一张表格,显示了encodeURI(),encodeURIComponent()escape()对常用符号 ASCII 字符的影响:

Char  encUrI  encURIComp  escape
*     *       *           *
.     .       .           .
_     _       _           _
-     -       -           -
~     ~       ~           %7E
'     '       '           %27
!     !       !           %21
(     (       (           %28
)     )       )           %29
/     /       %2F         /
+     +       %2B         +
@     @       %40         @
?     ?       %3F         %3F
=     =       %3D         %3D
:     :       %3A         %3A
#     #       %23         %23
;     ;       %3B         %3B
,     ,       %2C         %2C
$     $       %24         %24
&     &       %26         %26
      %20     %20         %20
%     %25     %25         %25
^     %5E     %5E         %5E
[     %5B     %5B         %5B
]     %5D     %5D         %5D
{     %7B     %7B         %7B
}     %7D     %7D         %7D
<     %3C     %3C         %3C
>     %3E     %3E         %3E
"     %22     %22         %22
\     %5C     %5C         %5C
|     %7C     %7C         %7C
`     %60     %60         %60

另一个重要的区别是它unescape()不处理多字节 UTF-8 序列,而decodeURI[Component]()处理:

decodeURIComponent("%C3%A9") == "é"
unescape("%C3%A9") == "é"
于 2016-06-06T10:19:32.393 回答
43
  • escape — broken, deprecated, do not use
  • encodeURI — encodes characters that are not allowed (raw) in URLs (use it to fix up broken URIs if you can't fix them beforehand)
  • encodeURIComponent — as encodeURI plus characters with special meaning in URIs (use it to encode data for inserting into a URI)
于 2013-01-14T11:51:41.483 回答
22

First of all - Escape is deprecated and shouldn't be used.

encodeURI()

You should use this when you want to encode a URL, it encodes symbols that is not allowed in a URL.

encodeURIComponent()

Should be used when you want to encode parameters of your URL, You can also use this to encode a whole URL. But you would have to decode it in order to use it again.

--

I'd say this a duplicate. Here's a good answer on SO - Credits goes to Arne Evertsson: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

There's a lot of details on why/why not on that topic.

于 2013-01-14T11:56:28.777 回答
7
  • escape- 已弃用,您不应该使用。

  • encodeURI- 替换所有字符,除了
    ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # a-z 0-9

  • encodeURIComponent- 替换所有字符,除了
    - _ . ! ~ * ' ( ) a-z 0-9
于 2016-05-19T12:47:21.827 回答
0

只是尝试encodeURI()encodeURIComponent()你自己...

console.log(encodeURIComponent('@#$%^&*'));

输入:@#$%^&*。输出:%40%23%24%25%5E%26*。所以,等等,发生了什么事*?为什么没有转换?TLDR:您实际上想要fixedEncodeURIComponent()fixedEncodeURI()。很长的故事...

警告:虽然 escape() 并未被严格弃用(如“从 Web 标准中删除”),但它在 ECMA-262 标准的附件 B 中定义,其介绍指出:

...在编写新的 ECMAScript 代码时,程序员不应使用或假设这些特性和行为的存在......

如果希望遵循更新的 RFC3986 的 URL,它保留了方括号(用于 IPv6),因此在形成可能是 URL 一部分的内容(例如主机)时不进行编码,以下代码片段可能会有所帮助:

function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }

为了更加严格地遵守 RFC 3986(保留 !、'、(、) 和 *),即使这些字符没有正式的 URI 分隔用途,也可以安全地使用以下内容:

function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }

fixedEncodeURI()那么问题可以简化:和有什么区别fixedEncodeURIComponent()fixedEncodeURIComponent()编码以下字符,而不编码fixedEncodeURI()+@?=:#;,$&.

于 2020-06-17T16:33:42.347 回答