escape(), encodeURI()和encodeURIComponent()(转)

 

escape(), encodeURI()和encodeURIComponent()是在Javascript中用于编码字符串的三个经常使用的方法,而他们之间的异同却困扰了不少的Javascript初学者,在这里对这三个方法详细地分析与比较一下。html

escape() 方法web

MSDN JScript Reference中如是说:服务器

The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as " ."ide

译:escape方法以Unicode格式返回一个包含传入参数内容的string类型的值。 Escape方法会将传入参数中全部的空格、标点符号、重音字符以及其它任何非ASCII字符替换为%xx的编码形式,其中xx与其所表示的字符的16进制数表示形式相同。如空格字符的16进制表示形式为0x20,则此时xx应为20,即escape(‘ ’) 返回“ ”。ui

Mozilla Developer Core Javascript Guide中如是说:编码

The escape and functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The function returns the ASCII string for the specified hexadecimal encoding value.spa

译:escape和方法可以帮助你编码和解码字符串。escape方法对于ISO Latin字符集中的字符组成的参数,返回其16进制编码。相对应的,方法则能将16进制编码形式的参数转化成为其ASCII码形式。code

encodeURI()方法component

MSDN JScript Reference中如是说:orm

The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters.

译:encodeURI方法返回一个通过编码的URI。若是将encodeURI方法的编码结果传递给decodeURI方法做参数,则能获得原始的未编码的字符串。须要注意到是encodeURI方法不编码以下字符":", "/", ";", and "?"。若是想要编码这些字符,请使用encodeURIComponent方法。

Mozilla Developer Core Javascript Guide中如是说:

Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

译:经过将每一个属于特定的字符集合的字符替换为一个、两个或者三个(为何是“一个、两个或者三个”本人也没有搞懂,望高人赐教)使用UTF-8编码来表示这个字符的escape序列来编码一个URI。如 ~!@#$%^&*(){}[]=:/,;?+\''"\\ 将被替换为 ~!@#$%^&*(){}[]=:/,;?+''"\

encodeURIComponent()方法

MSDN JScript Reference中如是说:

The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component.

译:encodeURIComponent方法返回一个编码过的URI。若是将encodeURIComponent方法的编码结果传递给encodeURIComponent方法做参数,则能获得原始的未编码的字符串。由于encodeURIComponent方法会编码全部的字符,因此若是待编码的字符串是用来表示一个路径(如/dir1/dir2/index.htm)时,就必定要当心使用了。‘/’符号会被其编码以后,将再也不是一个有效的路径标识符,因此不能被web服务器正确地识别。当字符串包含一个单独的URI component(指?后面的请求参数)的时候,请使用此方法。

Mozilla Developer Core Javascript Guide中如是说:

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certaincharacters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

译:经过将每一个属于特定的字符集合的字符替换为一个、两个或者三个(为何是“一个、两个或者三个”本人也没有搞懂,望高人赐教)使用UTF-8编码来表示这个字符的escape序列来编码一个URIComponent。

有什么区别?什么时候使用?


经过上面的介绍能够看出,MS的文档明显要比Mozilla详细、易懂一些,可是它们表达的都是一个意思。可是escape(), encodeURI()和 encodeURIComponent()有什么异同,它们分别适用于那种特定的状况呢?

escape方法并不编码字符+。而咱们知道,在用户提交的表单字段中,若是有空格,则会被转化为+字符,而服务器解析的时候则会认为+号表明空格。因为这个缺陷,escape方法并不能正确地处理全部的非ASCII字符,你应当尽可能避免使用escape方法,取而代之,你最好选择encodeURIComponent()方法。
escape()不编码的字符:@*/+

相对于使用escape方法,使用encodeURI方法会显得更专业一些。当你须要编码一整个URI的时候,你能够使用此方法,由于URI中的合法字符都不会被编码转换。须要注意到是字符’也是URI中的合法字符,因此也不会被编码转换。
encodeURI() 不编码的字符:
 ~!@#$&*()=:/,;?+''

encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最经常使用的。须要注意到是字符’也是URI中的合法字符,因此也不会被编码转换。
encodeURIComponent()不编码的字符: ~!*()''

相关文章
相关标签/搜索