简体   繁体   English

escape()、encodeURI()、encodeURIComponent()的区别

[英]Difference between escape(), encodeURI(), encodeURIComponent()

In JavaScript, what is the difference between these?在 JavaScript 中,它们之间有什么区别?

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

For the visually minded, here's a table showing the effects of encodeURI() , encodeURIComponent() and escape() on the commonly-used symbolic ASCII characters:对于有视觉意识的人,下面的表格显示了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

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") == "é"
  • escape — broken, deprecated, do not use escape - 已损坏,已弃用,请勿使用
  • encodeURI — encodes characters that are not allowed (raw) in URLs (use it to fix up broken URIs if you can't fix them beforehand) encodeURI — 对 URL 中不允许(原始)的字符进行编码(如果您无法事先修复它们,请使用它来修复损坏的 URI)
  • encodeURIComponent — as encodeURI plus characters with special meaning in URIs (use it to encode data for inserting into a URI) encodeURIComponent — 作为encodeURI加上 URI 中具有特殊含义的字符(使用它来编码数据以插入 URI)

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.当您想对 URL 的参数进行编码时应该使用它,您也可以使用它来编码整个 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?这是关于 SO 的一个很好的答案 - 归功于 Arne Evertsson: 您何时应该使用转义而不是 encodeURI / encodeURIComponent?

There's a lot of details on why/why not on that topic.关于为什么/为什么不在该主题上有很多详细信息。

  • escape - deprecated, you shouldn't use. escape - 已弃用,您不应使用。

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

  • encodeURIComponent - replaces all characters except encodeURIComponent - 替换所有字符,除了
    - _ . ! ~ * ' ( ) az 0-9

Just try encodeURI() and encodeURIComponent() yourself...只需自己尝试encodeURI()encodeURIComponent() ......

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

Input: @#$%^&* .输入: @#$%^&* Output: %40%23%24%25%5E%26* .输出: %40%23%24%25%5E%26* So, wait, what happened to * ?那么,等等, *发生了什么? Why wasn't this converted?为什么这个没有转换? TLDR: You actually want fixedEncodeURIComponent() and fixedEncodeURI() . TLDR:您实际上需要fixedEncodeURIComponent()fixedEncodeURI() Long-story...很长的故事...

Warning: Although escape() is not strictly deprecated (as in "removed from the Web standards"), it is defined in Annex B of the ECMA-262 standard, whose introduction states:警告:虽然escape() 并未被严格弃用(如“从Web 标准中移除”),但它在ECMA-262 标准的附录B 中进行了定义,其介绍指出:

... Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code.... ...程序员在编写新的 ECMAScript 代码时不应使用或假设这些功能和行为的存在......

If one wishes to follow the more recent RFC3986 for URLs, which makes square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following code snippet may help:如果希望遵循更新的 URL RFC3986,它保留方括号(用于 IPv6),因此在形成可能是 URL 一部分的内容(例如主机)时不进行编码,以下代码片段可能会有所帮助:

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

To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:为了更严格地遵守 RFC 3986(保留 !、'、(、) 和 *),即使这些字符没有正式的 URI 分隔用途,也可以安全地使用以下内容:

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

Then the question can be simplified: What's the difference between fixedEncodeURI() and fixedEncodeURIComponent() ?那么问题就可以简化了: fixedEncodeURI()fixedEncodeURIComponent()什么fixedEncodeURIComponent() fixedEncodeURIComponent() encodes the following characters, while fixedEncodeURI() does not: +@?=:#;,$& . fixedEncodeURIComponent()对以下字符进行编码,而fixedEncodeURI()不会: +@?=:#;,$&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript 转义、encodeURI 和 encodeURIComponent 的问题 - Issue with javascript escape, encodeURI and encodeURIComponent encodeURIComponent和encodeURI - encodeURIComponent and encodeURI encodeURIComponent和encodeURI之间互斥url编码? - Mutually exclusive url encoding between encodeURIComponent and encodeURI? 你什么时候应该使用escape而不是encodeURI / encodeURIComponent? - When are you supposed to use escape instead of encodeURI / encodeURIComponent? Javascript:encodeURI()/ encodeURIComponent()charset - Javascript: encodeURI() / encodeURIComponent() charset encodeURI和encodeUri有什么区别 - what is the difference between encodeURI and decodeUri 替换或避免 encodeURIComponent 或 encodeuri,encode,escape 将数据从客户端传递到服务器 - Replacement or avoid encodeURIComponent or encodeuri,encode,escape to pass data from client to server $ httpParamSerializerJQLike和encodeURIComponent之间的区别是什么? - What is the difference between $httpParamSerializerJQLike and encodeURIComponent 我应该使用 encodeURI 还是 encodeURIComponent 来编码 URL? - Should I use encodeURI or encodeURIComponent for encoding URLs? JSON字符串结果的ejs &lt;%=和encodeURI有什么区别? - What is the difference between ejs <%= and encodeURI for a JSON string result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM