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 OOP面向对象介绍
Dec 02 Javascript
jQuery UI Autocomplete 体验分享
Feb 14 Javascript
js 判断控件获得焦点的示例代码
Mar 04 Javascript
js兼容火狐获取图片宽和高的方法
May 21 Javascript
分析js闭包引起的事件注册问题
Mar 29 Javascript
AngularJS 单元测试(二)详解
Sep 21 Javascript
通过npm或yarn自动生成vue组件的方法示例
Feb 12 Javascript
js实现鼠标拖拽缩放div实例代码
Mar 25 Javascript
手把手教你使用TypeScript开发Node.js应用
May 06 Javascript
jquery+php后台实现省市区联动功能示例
May 23 jQuery
使用 node.js 模仿 Apache 小部分功能
Jul 07 Javascript
vue中使用腾讯云Im的示例
Oct 23 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/12/06 PHP
PHP获取数组中指定的一列实例
2017/12/27 PHP
通用JS事件写法实现代码
2009/01/07 Javascript
jQuery chili图片远处放大插件
2009/11/30 Javascript
javascript获取作用在元素上面的样式属性代码
2012/09/20 Javascript
JQuery对class属性的操作实现按钮开关效果
2013/10/11 Javascript
JS注释所产生的bug 即使注释也会执行
2013/11/19 Javascript
使用百度地图api实现根据地址查询经纬度
2014/12/11 Javascript
jQuery的事件委托实例分析
2015/07/15 Javascript
如何根据百度地图计算出两地之间的驾驶距离(两种语言js和C#)
2015/10/29 Javascript
JavaScript中rem布局在react中的应用
2015/12/09 Javascript
基于jQuery下拉选择框插件支持单选多选功能代码
2016/06/07 Javascript
jquery 判断selection range 是否在容器中的简单实例
2016/08/02 Javascript
Jquery Easyui进度条组件Progress使用详解(8)
2020/03/26 Javascript
过期软件破解办法实例详解
2017/01/04 Javascript
详解Angularjs 如何自定义Img的ng-load 事件
2017/02/15 Javascript
在Vue中使用icon 字体图标的方法
2019/06/14 Javascript
微信小程序左右滚动公告栏效果代码实例
2019/09/16 Javascript
JavaScript代码实现简单计算器
2020/12/27 Javascript
利用 python 对目录下的文件进行过滤删除
2017/12/27 Python
Python格式化输出字符串方法小结【%与format】
2018/10/29 Python
python批量下载抖音视频
2019/06/17 Python
python中 * 的用法详解
2019/07/10 Python
Python数据库小程序源代码
2019/09/15 Python
python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例
2020/03/06 Python
python 对象真假值的实例(哪些视为False)
2020/12/11 Python
CSS3正方体旋转示例代码
2013/08/08 HTML / CSS
css3中transition属性详解
2014/09/02 HTML / CSS
会计员岗位职责
2014/03/15 职场文书
中药学专业求职信
2014/05/31 职场文书
班训口号大全
2014/06/18 职场文书
廉政承诺书2015
2015/04/28 职场文书
2016年小学“感恩教师”主题队日活动总结
2016/04/01 职场文书
Mongo服务重启异常问题的处理方法
2021/07/01 MongoDB
spring boot中nativeQuery的用法
2021/07/26 Java/Android
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers