escape、encodeURI 和 encodeURIComponent 的区别


Posted in Javascript onMarch 02, 2009

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() 不编码的字符: ~!@#@{content}*()=:/,;?+'

encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符'也是URI中的合法字符,所以也不会被编码转换。

encodeURIComponent()不编码的字符: ~!*()'

Javascript 相关文章推荐
JavaScript使用技巧精萃[代码非常实用]
Nov 21 Javascript
JS保存和删除cookie操作 判断cookie是否存在
Nov 13 Javascript
表格奇偶行设置不同颜色的核心JS代码
Dec 24 Javascript
javascript实现删除前弹出确认框
Jun 04 Javascript
使用AngularJS创建自定义的过滤器的方法
Jun 18 Javascript
javascript表单处理具体实现代码(表单、链接、按钮)
May 07 Javascript
全面解析Javascript无限添加QQ好友原理
Jun 15 Javascript
详解Vue Elementui中的Tag与页面其它元素相互交互的两三事
Sep 25 Javascript
vue实现绑定事件的方法实例代码详解
Jun 20 Javascript
Cordova(ionic)项目实现双击返回键退出应用
Sep 17 Javascript
three.js利用gpu选取物体并计算交点位置的方法示例
Nov 25 Javascript
JavaScript中继承原理与用法实例入门
May 09 Javascript
javascript 文档的编码问题解决
Mar 01 #Javascript
jQuery Div中加载其他页面的实现代码
Feb 27 #Javascript
jQuery 使用个人心得
Feb 26 #Javascript
javascript div 弹出可拖动窗口
Feb 26 #Javascript
javascript URL锚点取值方法
Feb 25 #Javascript
javascript 特殊字符串
Feb 25 #Javascript
javascript 密码强弱度检测万能插件
Feb 25 #Javascript
You might like
PHP 读取Postgresql中的数组
2013/04/14 PHP
PHP实现中国公民身份证号码有效性验证示例代码
2017/05/03 PHP
php中通用的excel导出方法实例
2017/12/30 PHP
php弹出提示框的是实例写法
2019/09/26 PHP
广告代码静态化js通用函数
2007/05/09 Javascript
json 入门基础教程 推荐
2009/10/31 Javascript
JavaScript浏览器选项卡效果
2010/08/25 Javascript
jquery插件制作 自增长输入框实现代码
2012/08/17 jQuery
javascript实现捕捉键盘上按下的键
2015/05/05 Javascript
Bootstrap每天必学之基础排版
2015/11/20 Javascript
超实用的JavaScript表单代码段
2016/02/26 Javascript
php基于redis处理session的方法
2016/03/14 Javascript
微信小程序 刷新上拉下拉不会断详细介绍
2017/05/11 Javascript
JS点击图片弹出文件选择框并覆盖原图功能的实现代码
2017/08/25 Javascript
nodejs Assert中equal(),strictEqual(),deepEqual(),strictDeepEqual()比较
2017/09/18 NodeJs
Angular 项目实现国际化的方法
2018/01/08 Javascript
axios全局请求参数设置,请求及返回拦截器的方法
2018/03/05 Javascript
angularjs通过过滤器返回超链接的方法
2018/10/26 Javascript
Vue实现搜索结果高亮显示关键字
2019/05/28 Javascript
JavaScript中的连续赋值问题实例分析
2019/07/12 Javascript
centos 下面安装python2.7 +pip +mysqld
2014/11/18 Python
Python的Django框架中的select_related函数对QuerySet 查询的优化
2015/04/01 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
Python用Pillow(PIL)进行简单的图像操作方法
2017/07/07 Python
python二维列表一维列表的互相转换实例
2018/07/02 Python
使用pycharm设置控制台不换行的操作方法
2019/01/19 Python
python 抓包保存为pcap文件并解析的实例
2019/07/23 Python
Python学习笔记之错误和异常及访问错误消息详解
2019/08/08 Python
PyQt5多线程刷新界面防假死示例
2019/12/13 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
python如何提升爬虫效率
2020/09/27 Python
美德好少年事迹材料
2014/01/19 职场文书
招聘与培训专员岗位职责
2014/01/30 职场文书
住房抵押登记委托书
2014/09/27 职场文书
银行优秀员工推荐信
2015/03/24 职场文书
JS中forEach()、map()、every()、some()和filter()的用法
2022/05/11 Javascript