比較escape、encodeURI、encodeURIComponent

來源:互聯網
上載者:User

標籤:des   http   io   使用   ar   java   for   strong   sp   

估計很多前端工程師並不清楚escape,encodeURI, encodeURIComponent的區別,也不知道什麼時候該用哪個方法,以及這些方法為什麼要被用到,下面我主要來闡述一下這三個方法的區別以及用法。 
escape 方法: 

引用MSDN JScript: 
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格式的字串值。所有的“空格”、“標點”、“重音字元”以及任何非ASCII字元都會被替換為帶著%xx 編碼,這裡的xx是一個表示原來字元的16進位數字。例如一個空格會被替換為%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拉丁字元參數的16進位編碼,而 unescape 方法會為指定的16進位編碼返回ASCII 字元。 
看看例子吧 

Javascript代碼  
  1. escape(‘[email protected]#$%^&*(){}[]=:/,;?+\‘"\\‘)  
  2. //%7E%[email protected]%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C  


encode 方法: 

引用MSDN JScript: 
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 不對以下字元編碼: 
":", "/", ";", 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字元,來對URI編碼。 
例子: 

Javascript代碼  
  1. encodeURI(‘[email protected]#$%^&*(){}[]=:/,;?+\‘"\\‘)  
  2. //[email protected]#$%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會對更多的字元進行編碼,包括表示路徑的"/",所以當使用時,需要謹慎 

引用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.


這個解釋和encode一樣,實際上encodeURI 和 encodeURIComponent 差別就是一個是對更多的字元編碼,而一個只是對URI部分編碼。 
同樣字串的例子: 

Java代碼  
  1. escape(‘[email protected]#$%^&*(){}[]=:/,;?+\‘"\\‘)  
  2. //%7E%[email protected]%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C  
  3. encodeURI(‘[email protected]#$%^&*(){}[]=:/,;?+\‘"\\‘);  
  4. //[email protected]#$%25%5E&*()%7B%7D%5B%5D=:/,;?+‘%22%5C  
  5. encodeURIComponent(‘[email protected]#$%^&*(){}[]=:/,;?+\‘"\\‘);  
  6. //~!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%2C%3B%3F%2B‘%22%5C  


總結: 
該怎麼使用呢? 

escape: 
escape 不會編碼的字元:@*/+ 
escape方法不回編碼+字元,+字元在伺服器端會被解釋成空格,這點和通過表達提交一樣。 
由於escape有這樣的缺點,和它不能很好的正確處理非ASCII字元的事實,我們應該盡量避免(對URI)使用escape,最好的方式是encodeURIComponent。 

encodeURI: 
encodeURI 不會編碼的字元很多,有:[email protected]#$&*()=:/,;?+‘ 
在對一段URI編碼來說,encodeURI方法比escape方法更專業一些。當你需要編碼一整個URI的時候,你可以使用此方法,因為URI中的合法字元都不會被編碼轉換。需要注意到是字元’也是URI中的合法字元,所以也不會被編碼轉換。 

encodeURIComponent: 
encodeURIComponent不會編碼的字元: ~!*()‘ 
encodeURIComponent方法在編碼單個URIComponent(指請求參數)應當是最常用的。需要注意到是字元’也是URI中的合法字元,所以也不會被編碼轉換。 


我的小結: 
其實前端最常用的是encodeURIComponent,因為這個方法會把URI中的queryString中的非法字元編碼,這樣通過get請求向伺服器端傳遞參數的時候不會出錯。 
escape 在後端和前端的介面規約裡常常會碰到,但是一般不需要前端在本地escape,一般是後端介面吐出 escape後的資料,前端unescape。 
此文章是翻譯而來 

原文:http://xkr.us/articles/javascript/encode-compare/ 
關於URL,URI不太明白的同學可以看看 http://www.ibm.com/developerworks/cn/xml/x-urlni.html 
這篇文章 

比較escape、encodeURI、encodeURIComponent

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.