Skip to content

[js] 第121天 请说说escape、encodeURI、decodeURI、encodeURIComponent和decodeURIComponent的区别? #1067

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第121天 请说说escape、encodeURI、decodeURI、encodeURIComponent和decodeURIComponent的区别?

Activity

93xiaoli

93xiaoli commented on Aug 14, 2019

@93xiaoli

之前完全不知道 查了才知道的

  • escape
    用来编码string,现在不推荐使用,注意无法处理特殊字符* @ - _ + . /
  • encodeURI
    用来编译URI,无法编码A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #,因为没法编译"&", "+", "=",无法完成get和post指令,但encodeURIComponent可以编译
  • encodeURIComponent
    无法编码 A-Z a-z 0-9 - _ . ! ~ * ' ( )
    我没有查到urlencodeComponent和urlencode,等看着大神的解答。谢谢@DarthVaderrr指正
xxf1996

xxf1996 commented on Aug 15, 2019

@xxf1996

@haizhilin2013 我也没查到urlencodeComponenturlencode这两个方法;是不是encodeURIComponentencodeURI

changed the title [-][js] 第121天 请说说escape、encodeURI、urlencodeComponent和urlencode的区别?[/-] [+][js] 第121天 请说说escape、encodeURI、decodeURI、encodeURIComponent和decodeURIComponent的区别?[/+] on Aug 15, 2019
haizhilin2013

haizhilin2013 commented on Aug 15, 2019

@haizhilin2013
CollaboratorAuthor

@93xiaoli @xxf1996 多谢提醒,我写错了!下次我会注意,多验证下!

ghost

ghost commented on Aug 15, 2019

@ghost

题目与#574 基本类似。

我发表于 #574 的答案:

  • encodeURI():将 URI 中的每个字符编码为 1-4 个格式为 %xx 的转义序列(xx 为十六进制数),但不包括 ASCII 数字、字母、URL 分隔符(/?,&、...)、以及其他部分 ASCII 字符。具体见 MDN
    • Example: encodeURI('http://example.com/端点?键=值') => 'http://example.com/%E7%AB%AF%E7%82%B9?%E9%94%AE=%E5%80%BC'
  • decodeURI():将已经编码的 URI 中的转义序列解码为它们表示的字符,但除了 encodeURI() 不会编码的字符。
  • encodeURIComponent():用于编码 URI 中的组成部分。它除了转义 encodeURI() 指定的字符,还会转义 URL 分隔符(/?,&、...)
    • Example: encodeURIComponent('测试/测试?测试=测试') => '%E6%B5%8B%E8%AF%95%2F%E6%B5%8B%E8%AF%95%3F%E6%B5%8B%E8%AF%95%3D%E6%B5%8B%E8%AF%95'
  • decodeURIComponent():将已经编码的 URI 组成部分中的转义序列解码为它们表示的字符,但除了 encodeURIComponent() 不会编码的字符。

另外, escape() 将会把字符串中除了字母、数字以及一部分符号以外的所有转义为 %XX 格式的转义序列,可以通过 unescape() 转回。它们是已经被弃用的方法,不建议使用。

对于之前的错误题目:
urlencode 大概指的是 MIME application/x-www-form-urlencoded,它将键值对信息序列化为 key1=value1&key2=value2&... 的形式,并且可以连接在 url 后面。

haizhilin2013

haizhilin2013 commented on Aug 15, 2019

@haizhilin2013
CollaboratorAuthor

@t532 好的,收到!

DarthVaderrr

DarthVaderrr commented on Aug 16, 2019

@DarthVaderrr

上面有小伙伴提到加密,需要注意的是这些都是编码方法,不是加密方法,前端无法对数据进行加密,只能编码,也就是说,任何懂前端技术的人都可以无障碍地获得数据原来的信息,因为这些编码都是可逆的,编码规则是恒定的

nowherebutup

nowherebutup commented on Oct 17, 2019

@nowherebutup

escape是之前的转义解码的函数,
encodeURI的范围比encodeURIComponent要小,

zhaofeipeter

zhaofeipeter commented on Jul 30, 2020

@zhaofeipeter

1、如果只是编码字符串,不和URL有半毛钱关系,那么用escape,而且这个方法一般不会用到。
2、如果你需要编码整个URL,然后需要使用这个URL,那么用encodeURI。
3、当你需要编码URL中的参数的时候,那么encodeURIComponent是最好方法。

xiaoqiangz

xiaoqiangz commented on Jul 7, 2022

@xiaoqiangz

escape 用来编码字符串的,不过已经在web标准中被删除了,如果要用尽量使用encodeUrl,如果要编码url要使用 encodeUrl, 如果有分隔符或其他特殊字符就要使用encodeURIComponent,但是该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haizhilin2013@xiaoqiangz@xxf1996@zhaofeipeter@93xiaoli

        Issue actions

          [js] 第121天 请说说escape、encodeURI、decodeURI、encodeURIComponent和decodeURIComponent的区别? · Issue #1067 · haizlin/fe-interview