在JavaScript中,有什么区别
escape / unescape
encodeuri / decodeuri
encodeURIComponent / decodeURIComponent
对于视觉上的头脑,这里有一个表格,显示 encodeURI , encodeURIComponent 和 escape 对常用的符号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") == "é"
escape - 已损坏,已弃用,请勿使用
encodeURI - 对URL中不允许(原始)的字符进行编码(如果您事先无法修复,请使用它来修复损坏的URI)
encodeURIComponent - as encodeURI 加上URI中具有特殊含义的字符(用它来编码数据以插入URI)
首先 - Escape已弃用,不应使用 .
encodeURI()
当您想要对URL进行编码时,您应该使用它,它会对URL中不允许的符号进行编码 .
encodeURIComponent()
当您想要编码URL的参数时,应该使用它,您也可以使用它来编码整个URL . 但你必须解码它才能再次使用它 .
我是一个很好的答案 - 致Arne Evertsson致辞:When are you supposed to use escape instead of encodeURI / encodeURIComponent?
关于为什么/为什么不在该主题上有很多细节 .
escape - 已弃用,您不应该使用 .
encodeURI - 替换除以外的所有字符; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # a-z 0-9
; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # a-z 0-9
encodeURIComponent - 替换除以外的所有字符- _ . ! ~ * ' ( ) a-z 0-9
- _ . ! ~ * ' ( ) a-z 0-9
4 回答
对于视觉上的头脑,这里有一个表格,显示
encodeURI
,encodeURIComponent
和escape
对常用的符号ASCII字符的影响:另一个重要区别是unescape不处理多字节UTF-8序列,而decodeURI [Component]则:
escape
- 已损坏,已弃用,请勿使用encodeURI
- 对URL中不允许(原始)的字符进行编码(如果您事先无法修复,请使用它来修复损坏的URI)encodeURIComponent
- asencodeURI
加上URI中具有特殊含义的字符(用它来编码数据以插入URI)首先 - Escape已弃用,不应使用 .
encodeURI()
当您想要对URL进行编码时,您应该使用它,它会对URL中不允许的符号进行编码 .
encodeURIComponent()
当您想要编码URL的参数时,应该使用它,您也可以使用它来编码整个URL . 但你必须解码它才能再次使用它 .
我是一个很好的答案 - 致Arne Evertsson致辞:When are you supposed to use escape instead of encodeURI / encodeURIComponent?
关于为什么/为什么不在该主题上有很多细节 .
escape
- 已弃用,您不应该使用 .encodeURI
- 替换除以外的所有字符; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # a-z 0-9
encodeURIComponent
- 替换除以外的所有字符- _ . ! ~ * ' ( ) a-z 0-9