簡體   English   中英

escape()、encodeURI()、encodeURIComponent()的區別

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

在 JavaScript 中,它們之間有什么區別?

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

對於有視覺意識的人,下面的表格顯示了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") == "é"
  • escape - 已損壞,已棄用,請勿使用
  • encodeURI — 對 URL 中不允許(原始)的字符進行編碼(如果您無法事先修復它們,請使用它來修復損壞的 URI)
  • encodeURIComponent — 作為encodeURI加上 URI 中具有特殊含義的字符(使用它來編碼數據以插入 URI)

首先 - Escape 已被棄用,不應使用。

編碼URI()

當您想對 URL 進行編碼時應該使用它,它會編碼 URL 中不允許的符號。

編碼URI組件()

當您想對 URL 的參數進行編碼時應該使用它,您也可以使用它來編碼整個 URL。 但是您必須對其進行解碼才能再次使用它。

——

我會說這是重復的。 這是關於 SO 的一個很好的答案 - 歸功於 Arne Evertsson: 您何時應該使用轉義而不是 encodeURI / encodeURIComponent?

關於為什么/為什么不在該主題上有很多詳細信息。

  • escape - 已棄用,您不應使用。

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

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

只需自己嘗試encodeURI()encodeURIComponent() ......

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

輸入: @#$%^&* 輸出: %40%23%24%25%5E%26* 那么,等等, *發生了什么? 為什么這個沒有轉換? TLDR:您實際上需要fixedEncodeURIComponent()fixedEncodeURI() 很長的故事...

警告:雖然escape() 並未被嚴格棄用(如“從Web 標准中移除”),但它在ECMA-262 標准的附錄B 中進行了定義,其介紹指出:

...程序員在編寫新的 ECMAScript 代碼時不應使用或假設這些功能和行為的存在......

如果希望遵循更新的 URL RFC3986,它保留方括號(用於 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() fixedEncodeURIComponent()對以下字符進行編碼,而fixedEncodeURI()不會: +@?=:#;,$&

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM