escape/encodeURI/encodeURIComponent的区别

示例

先看一个例子:

var string = '~!@#$%^&*(){}[]=:/,;?+\'"\\';
console.log(escape(string) + '\n' + encodeURI(string) + '\n' + encodeURIComponent(string));

输出结果:

%7E%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%2C%3B%3F+%27%22%5C
~!@#$%25%5E&*()%7B%7D%5B%5D=:/,;?+'%22%5C
~!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%2C%3B%3F%2B'%22%5C 

区别

escape() will not encode: @*/+
encodeURI() will not encode: ~!@#$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'

参考资料

http://jotcmd.com/96/escape-encodeuri-compare/
http://xkr.us/articles/javascript/encode-compare/

标签: none

添加新评论