escape、encodeURI 和 encodeURIComponent 的區(qū)別
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格式返回一個包含傳入?yún)?shù)內(nèi)容的string類型的值。 Escape方法會將傳入?yún)?shù)中所有的空格、標點符號、重音字符以及其它任何非ASCII字符替換為%xx的編碼形式,其中xx與其所表示的字符的16進制數(shù)表示形式相同。如空格字符的16進制表示形式為0x20,則此時xx應(yīng)為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字符集中的字符組成的參數(shù),返回其16進制編碼。相對應(yīng)的,unescape方法則能將16進制編碼形式的參數(shù)轉(zhuǎn)化成為其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方法返回一個經(jīng)過編碼的URI。如果將encodeURI方法的編碼結(jié)果傳遞給decodeURI方法作參數(shù),則能得到原始的未編碼的字符串。需要注意到是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方法的編碼結(jié)果傳遞給encodeURIComponent方法作參數(shù),則能得到原始的未編碼的字符串。因為encodeURIComponent方法會編碼所有的字符,所以如果待編碼的字符串是用來表示一個路徑(如/dir1/dir2/index.htm)時,就一定要小心使用了?!?'符號會被其編碼之后,將不再是一個有效的路徑標識符,所以不能被web服務(wù)器正確地識別。當字符串包含一個單獨的URI component(指?后面的請求參數(shù))的時候,請使用此方法。
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。
有什么區(qū)別?何時使用?
通過上面的介紹可以看出,MS的文檔明顯要比Mozilla詳細、易懂一些,但是它們表達的都是一個意思。但是escape(), encodeURI()和 encodeURIComponent()有什么異同,它們分別適用于那種特定的情況呢?
escape方法并不編碼字符+。而我們知道,在用戶提交的表單字段中,如果有空格,則會被轉(zhuǎn)化為+字符,而服務(wù)器解析的時候則會認為+號代表空格。由于這個缺陷,escape方法并不能正確地處理所有的非ASCII字符,你應(yīng)當盡量避免使用escape方法,取而代之,你最好選擇encodeURIComponent()方法。
escape()不編碼的字符:@*/+
相對于使用escape方法,使用encodeURI方法會顯得更專業(yè)一些。當你需要編碼一整個URI的時候,你可以使用此方法,因為URI中的合法字符都不會被編碼轉(zhuǎn)換。需要注意到是字符'也是URI中的合法字符,所以也不會被編碼轉(zhuǎn)換。
encodeURI() 不編碼的字符: ~!@#@{content}*()=:/,;?+'
encodeURIComponent方法在編碼單個URIComponent(指請求參數(shù))應(yīng)當是最常用的。需要注意到是字符'也是URI中的合法字符,所以也不會被編碼轉(zhuǎn)換。
encodeURIComponent()不編碼的字符: ~!*()'
- js中編碼函數(shù):escape,encodeURI與encodeURIComponent詳解
- js中字符串編碼函數(shù)escape()、encodeURI()、encodeURIComponent()區(qū)別詳解
- 談?wù)別ncodeURI和encodeURIComponent以及escape的區(qū)別與應(yīng)用
- 深入分析escape()、encodeURI()、encodeURIComponent()的區(qū)別及示例
- javascript 字符 Escape,encodeURI,encodeURIComponent
- URL編碼轉(zhuǎn)換,escape() encodeURI() encodeURIComponent()
- escape、encodeURI、encodeURIComponent等方法的區(qū)別比較
- 簡單明了區(qū)分escape、encodeURI和encodeURIComponent
相關(guān)文章
微信小程序?qū)崿F(xiàn)點餐小程序左側(cè)滑動菜單
這篇文章主要為大家詳細介紹了微信小程序?qū)崿F(xiàn)點餐小程序左側(cè)滑動菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07JS動態(tài)添加元素及綁定事件造成程序重復執(zhí)行解決
這篇文章主要給大家介紹了關(guān)于JS動態(tài)添加元素及綁定事件造成程序重復執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-12-12Bootstrap 表單驗證formValidation 實現(xiàn)遠程驗證功能
這篇文章主要介紹了Bootstrap 表單驗證formValidation 實現(xiàn)遠程驗證功能,需要的朋友可以參考下2017-05-05uniapp使用uni.chooseLocation()打開地圖選擇位置詳解
這篇文章主要給大家介紹了關(guān)于uniapp使用uni.chooseLocation()打開地圖選擇位置的相關(guān)資料,因為最近在項目中遇到一個要用戶授權(quán)位置且可以用戶自己選擇位置的功能,需要的朋友可以參考下2023-06-06