Javascript escape()、encodeURI()和encodeURIComponent()之间的差异

Javascript escape()、encodeURI()和encodeURIComponent()之间的差异,javascript,Javascript,在JavaScript中,这两者之间有什么区别 escape()/unescape() encodeuri()/decodeuri() encodeURIComponent()/decodeURIComponent() escape-已损坏、不推荐使用,请勿使用 encodeURI-对URL中不允许(原始)的字符进行编码(如果您无法事先修复损坏的URI,请使用它来修复它们) encodeURIComponent-作为encodeURI加上URI中具有特殊含义的字符(用于编码数据以插入URI)

在JavaScript中,这两者之间有什么区别

  • escape()
    /
    unescape()
  • encodeuri()
    /
    decodeuri()
  • encodeURIComponent()
    /
    decodeURIComponent()
    • escape
      -已损坏、不推荐使用,请勿使用
    • encodeURI
      -对URL中不允许(原始)的字符进行编码(如果您无法事先修复损坏的URI,请使用它来修复它们)
    • encodeURIComponent
      -作为
      encodeURI
      加上URI中具有特殊含义的字符(用于编码数据以插入URI)

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

    encodeURI()

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

    encodeURIComponent()

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

    --

    我认为这是一个复制品。以下是关于SO的一个很好的答案——Arne Evertsson的学分:

    关于为什么/为什么不谈这个话题,有很多细节。

    • escape
      -已弃用,不应使用

    • encodeURI
      -替换除
      ;,/?:@&=+$-。!~*'()#a-z 0-9

    • encodeURIComponent
      -替换除
      -。!~*'()a-z 0-9

    对于有视觉思维的人,这里有一个表格,显示了
    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
    
    只需自己尝试一下
    encodeURI()
    encodeURIComponent()


    console.log(encodeURIComponent('@$%^&*')相关问题有帮助吗?可能重复的一个主要区别是
    encodeURI
    不会编码
    /
    ,因此:
    encodeURIComponent(“ac/dc”)
    =>
    ac%2Fdc
    encodeURI(“ac/dc”)
    ac/dc
    关于如何使用它们的示例将非常有用。例如,
    encodeURI('http://something.com?whatever)
    'http://example.com?something=“+encodeURIComponent(userInputVariable)
    decodeURIComponent("%C3%A9") == "é"
    unescape("%C3%A9") == "é"