# escape、encodeURI、encodeURIComponent 的区别
escape/unescape
-
escape: 返回一个经过Unicode编码后的字符串:
- 语法:excape(str);
- Escape方法会将传入参数中所有的空格、标点符号、重音字符以及其它任何非ASCII字符替换为%xx的编码形式;
- 字符的16进制格式值,当该值小于等于0xFF时,用一个2位转移序列: %xx 表示. 大于的话则使用4位序列:%uxxxx 表示;
- 如空格字符的16进制表示形式为0x20,则此时xx应为20,即escape(’ ') 返回“%20”
- escape 函数是全局对象的属性. 特色字符如: @*_±./ 被排除在外.
escape('a=123'); // "a%3D123" escape("äöü"); // "%E4%F6%FC" // 不转换@*_+-./ escape("@*_+-./"); // "@*_+-./"
-
unescape则对escape编码的字符串进行解码
unexcape("a%3D123"); // "a=123"
encodeURI/decodeURI
-
encodeURI方法返回一个经过编码的URI;
- encodeURI不编码的字符:保留字符(; , / ? : @ & = + $),非转义的字符(字母 数字 - _ . ! ~ * ’ ( )),数字符号(#)
- encodeURI不会对 “&”, “+”, 和 “=” 进行编码,因此无法产生http get/post请求的uri
encodeURI('a=123&b=32@3'); // "a=123&b=32@3"
-
decodeURI对encodeURI编码的字符串进行解码;
encodeURIComponent/decodeURIComponent
-
encodeURIComponent方法返回一个编码过的URI:
- encodeURIComponent不编码的字符:~!*()’
encodeURIComponent('a=123&b=32@'); // "a%3D123%26b%3D32%40" encodeURIComponent('a=er~23&b=34!*()'); // "a%3Der~23%26b%3D34!*()"
-
decodeURIComponent对encodeURIComponent编码的字符串进行解码;