Difference between escape(), encodeURI(), encodeURIComponent()

Difference between escape(), encodeURI(), encodeURIComponent()

回答1

For the visually minded, here's a table showing the effects of encodeURI(), encodeURIComponent() and escape() on the commonly-used symbolic ASCII characters:

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:

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

 

回答2

  • 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)

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(26)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-09-02 How to Publish a NuGet Package
2015-09-02 Three ways to do WCF instance management
2015-09-02 WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
点击右上角即可分享
微信分享提示