Javascript 转义、encodeuri、encodeURIComponent 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14317861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:21:39  来源:igfitidea点击:

difference between escape, encodeuri, encodeURIComponent

javascript

提问by jatin patil

In JavaScript, what is the difference between

在 JavaScript 中,有什么区别

  1. escape / unescape
  2. encodeuri / decodeuri
  3. encodeURIComponent / decodeURIComponent
  1. 逃避/逃避
  2. 编码器/解码器
  3. encodeURIComponent / decodeURIComponent

回答by chris

For the visually minded, here's a table showing the effects of encodeURI, encodeURIComponentand escapeon the commonly-used symbolic ASCII characters:

对于有视觉意识的人,这里有一个表格,显示了encodeURI,encodeURIComponentescape对常用符号 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

Another vital difference is that unescape does not handle multi-byte UTF-8 sequences whereas decodeURI[Component] does:

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

decodeURIComponent("%C3%A9") == "é"
unescape("%C3%A9") == "??"

回答by Quentin

  • 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 encodeURIplus characters with special meaning in URIs (use it to encode data for inserting into a URI)
  • escape— 已损坏、已弃用、请勿使用
  • encodeURI— 对 URL 中不允许(原始)的字符进行编码(如果您不能事先修复它们,请使用它来修复损坏的 URI)
  • encodeURIComponent— 作为encodeURIURI 中具有特殊含义的加号字符(使用它来编码数据以插入 URI)

回答by Peter Rasmussen

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

首先 - Escape 已被弃用,不应使用。

encodeURI()

编码URI()

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

当您想对 URL 进行编码时应该使用它,它会编码 URL 中不允许的符号。

encodeURIComponent()

编码URI组件()

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.

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

--

——

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?

我会说这是重复的。这是关于 SO 的一个很好的答案 - 归功于 Arne Evertsson: 您什么时候应该使用转义而不是 encodeURI / encodeURIComponent?

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

关于为什么/为什么不在该主题上有很多详细信息。

回答by dmo

  • escape- deprecated, you shouldn't use.

  • encodeURI- replaces all characters except
    ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # a-z 0-9

  • encodeURIComponent- replaces all characters except
    - _ . ! ~ * ' ( ) a-z 0-9
  • escape- 已弃用,您不应使用。

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

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