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 AOP 实现ajax回调函数使用比较方便
Nov 20 Javascript
浅谈Javascript中的Function与Object
Jan 26 Javascript
javascript自定义滚动条实现代码
Apr 20 Javascript
js制作简单的音乐播放器的示例代码
Aug 28 Javascript
微信小程序之页面跳转和参数传递的实现
Sep 29 Javascript
vue-router3.0版本中 router.push 不能刷新页面的问题
May 10 Javascript
Vue中父子组件通讯之todolist组件功能开发
May 21 Javascript
vue中$set的使用(结合在实际应用中遇到的坑)
Jul 10 Javascript
使用 js 简单的实现 bind、call 、aplly代码实例
Sep 07 Javascript
javascript 构建模块化开发过程解析
Sep 11 Javascript
vue实现吸顶、锚点和滚动高亮按钮效果
Oct 21 Javascript
使用PreloadJS加载图片资源的基础方法详解
Feb 03 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 字符串函数收集
2010/03/29 PHP
Thinkphp实现MySQL读写分离操作示例
2014/06/25 PHP
利用PHP访问带有密码的Redis方法示例
2017/02/09 PHP
解决laravel-admin 自己新建页面里 js 需要刷新一次的问题
2019/10/03 PHP
发布BlueShow v1.0 图片浏览器(类似lightbox)blueshow.js 打包下载
2007/07/21 Javascript
JavaScript 组件之旅(二)编码实现和算法
2009/10/28 Javascript
javascript或asp实现的判断身份证号码是否正确两种验证方法
2009/11/26 Javascript
基于jQuery制作迷你背词汇工具
2010/07/27 Javascript
ASP中Sub和Function的区别说明
2020/08/30 Javascript
jquery滚动组件(vticker.js)实现页面动态数据的滚动效果
2013/07/03 Javascript
JavaScript网页定位详解
2014/01/13 Javascript
javascript中的Function.prototye.bind
2015/06/25 Javascript
深入探讨javascript函数式编程
2015/10/11 Javascript
基于Javascript实现返回顶部按钮
2016/02/29 Javascript
js实现简单的计算器功能
2017/01/16 Javascript
Angular2使用Angular CLI快速搭建工程(一)
2017/05/21 Javascript
JavaScript之promise_动力节点Java学院整理
2017/07/03 Javascript
python命令行参数sys.argv使用示例
2014/01/28 Python
python实现得到一个给定类的虚函数
2014/09/28 Python
介绍Python的Django框架中的静态资源管理器django-pipeline
2015/04/25 Python
使用Python内置的模块与函数进行不同进制的数的转换
2016/03/12 Python
Python中的time模块与datetime模块用法总结
2016/06/30 Python
Python计算斗牛游戏概率算法实例分析
2017/09/26 Python
Python 和 JS 有哪些相同之处
2017/11/23 Python
Python Matplotlib 基于networkx画关系网络图
2019/07/10 Python
Python使用matplotlib绘制三维参数曲线操作示例
2019/09/10 Python
python属于解释语言吗
2020/06/11 Python
安装pyinstaller遇到的各种问题(小结)
2020/11/20 Python
介绍一下Make? 为什么使用make
2016/07/31 面试题
个人简历自荐信
2013/12/05 职场文书
行政主管职责范本
2014/03/07 职场文书
女生抽烟检讨书
2014/10/05 职场文书
道士塔读书笔记
2015/06/30 职场文书
网络研修随笔感言
2015/11/18 职场文书
Ajax 的初步实现(使用vscode+node.js+express框架)
2021/06/18 Javascript
MySQL非空约束(not null)案例讲解
2021/08/23 MySQL