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 判断指定字符串是否为有效数字
May 11 Javascript
通过jquery的$.getJSON做一个跨域ajax请求试验
May 03 Javascript
到处都是jQuery选择器的年代 不了解它们的性能,行吗
Jun 18 Javascript
json实现前后台的相互传值详解
Jan 05 Javascript
js实现右下角提示框的方法
Feb 03 Javascript
整理Javascript流程控制语句学习笔记
Nov 29 Javascript
实例详解JavaScript中setTimeout函数的执行顺序
Jul 12 Javascript
VUE实现表单元素双向绑定(总结)
Aug 08 Javascript
每天学点Vue源码之vm.$mount挂载函数
Mar 11 Javascript
js实现全选反选不选功能代码详解
Apr 24 Javascript
JavaScript Window窗口对象属性和使用方法
Jan 19 Javascript
vue addRoutes路由动态加载操作
Aug 04 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
自动跳转中英文页面
2006/10/09 PHP
NO3第三帝国留言簿制作过程
2006/10/09 PHP
php array_map()数组函数使用说明
2011/07/12 PHP
CI框架中site_url()和base_url()的区别
2015/01/07 PHP
php获取文章内容第一张图片的方法示例
2017/07/03 PHP
php apache开启跨域模式过程详解
2019/07/08 PHP
xml文档转换工具,附图表例子(hta)
2010/11/17 Javascript
js网页侧边随页面滚动广告效果实现
2011/04/14 Javascript
JQuery动态创建DOM、表单元素的实现代码
2011/08/09 Javascript
jQuery EasyUI API 中文文档 - ComboGrid 组合表格
2011/10/13 Javascript
Js 回车换行处理的办法及replace方法应用
2013/01/24 Javascript
fckeditor粘贴Word时弹出窗口取消的方法
2014/10/30 Javascript
jquery复选框多选赋值给文本框的方法
2015/01/27 Javascript
jquery通过name属性取值的简单实现方法
2016/06/20 Javascript
knockoutjs动态加载外部的file作为component中的template数据源的实现方法
2016/09/01 Javascript
基于JavaScript实现验证码功能
2017/04/01 Javascript
基于JavaScript实现的顺序查找算法示例
2017/04/14 Javascript
node.js-v6新版安装具体步骤(分享)
2017/09/06 Javascript
详解Chai.js断言库API中文文档
2018/01/31 Javascript
VUE 配置vue-devtools调试工具及安装方法
2018/09/30 Javascript
bootstrap-table formatter 使用vue组件的方法
2019/05/09 Javascript
Python模拟登录12306的方法
2014/12/30 Python
Python 错误和异常代码详解
2018/01/29 Python
python pandas库中DataFrame对行和列的操作实例讲解
2018/06/09 Python
pycharm运行程序时在Python console窗口中运行的方法
2018/12/03 Python
python退出命令是什么?详解python退出方法
2018/12/10 Python
Python交互式图形编程的实现
2019/07/25 Python
python的移位操作实现详解
2019/08/21 Python
基于Python共轭梯度法与最速下降法之间的对比
2020/04/02 Python
英国办公用品商店:Office Outlet
2018/04/04 全球购物
古汉语文学求职信范文
2014/03/16 职场文书
大学新闻系求职信
2014/06/03 职场文书
关于安全的广播稿
2014/10/23 职场文书
先进教师个人主要事迹材料
2015/11/03 职场文书
Django drf请求模块源码解析
2021/06/08 Python
Redis的字符串是如何实现的
2021/10/24 Redis