escape()、encodeURI()和encodeURIComponent()之间的区别

47
在JavaScript中,以下这些有什么区别?
  1. escape() / unescape()
  2. encodeuri() / decodeuri()
  3. encodeURIComponent() / decodeURIComponent()

相关的问题有帮助吗? - John Dvorak
http://xkr.us/articles/javascript/encode-compare/ - maxwell
2
可能是https://dev59.com/OXVD5IYBdhLWcg3wI3-L?rq=1的重复问题。 - John Dvorak
一个主要的区别是encodeURI不会对斜杠 / 进行编码,因此:encodeURIComponent("ac/dc") => ac%2FdcencodeURI("ac/dc") => ac/dc - user993683
5个回答

72

对于具有视觉思维的人,这里是一张表格,显示常用符号ASCII字符经过encodeURI()encodeURIComponent()escape()处理后的效果:

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") == "é"

46
  • escape — 已经废弃,不要使用
  • encodeURI — 对在URL中不能使用(原始的)字符进行编码(如果无法事先修复它们,请使用它来修复损坏的URI)
  • encodeURIComponent — 与encodeURI相同,还可对在URI中具有特殊意义的字符进行编码(使用它来对数据进行编码以便插入到URI中)

3
例如,encodeURI('http://something.com?whatever')'http://example.com?something=' + encodeURIComponent(userInputVariable) 等等。如果能提供使用示例将会非常有帮助。 - Joe Phillips

26

首先 - Escape已经过时,不应该使用。

encodeURI()

当您想对URL进行编码时,应使用此函数,它可以对URL中不允许的符号进行编码。

encodeURIComponent()

当您想对URL参数进行编码时,应使用此函数。您也可以使用它来对整个URL进行编码,但是您必须解码它才能再次使用。

--

我认为这是一个重复问题。以下是SO上的好答案-由Arne Evertsson提供:什么情况下应该使用escape而不是encodeURI / encodeURIComponent?

有关该主题的原因和不应该的详细信息。


9
  • escape - 已过时,不应再使用。

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

  • encodeURIComponent - 替换除了
    - _ . ! ~ * ' ( ) a-z 0-9以外的所有字符。

1

只需尝试 encodeURI()encodeURIComponent() 就行...

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

输入:@#$%^&*。输出:%40%23%24%25%5E%26*。那么,等等,* 怎么没被转换?为什么没有被转换?简而言之:你实际上需要 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()则不编码:+@?=:#;,$&


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接