Javascript中escape(), encodeURI()和encodeURIComponent()之精析與比較

來源:互聯網
上載者:User

 

escape(), encodeURI()和encodeURIComponent()是在Javascript中用於編碼字串的三個常用的方法,而他們之間的異同卻困擾了很多的Javascript初學者,今天我就在這裡對這三個方法詳細地分析與比較一下。


escape() 方法

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 "%20."

鄙人譯:escape方法以Unicode格式返回一個包含傳入參數內容的string類型的值。 Escape方法會將傳入參數中所有的空格、標點符號、重音字元以及其它任何非ASCII字元替換為%xx的編碼形式,其中xx與其所表示的字元的16進位數表示形式相同。如空白字元的16進位表示形式為0x20,則此時xx應為20,即escape(‘ ’) 返回“%20”。

Mozilla Developer Core Javascript Guide中如是說:

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

鄙人譯:escape和unescape方法能夠協助你編碼和解碼字串。escape方法對於ISO Latin字元集中的字元組成的參數,返回其16進位編碼。相對應的,unescape方法則能將16進位編碼形式的參數轉化成為其ASCII碼形式。


encodeURI()方法

MSDN JScript Reference中如是說:

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。如 ~!@#$%^&*(){}[]=:/,;?+\'"\\ 將被替換為 ~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C


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 certain characters 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()不編碼的字元: ~!*()'

 

來源:http://blog.csdn.net/PiouseLeo/archive/2008/01/28/2069618.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.