javascript中escape、encodeURI和encodeURIComponent

本文详细介绍了JavaScript中三种不同的URL编码方法:escape(), encodeURI(), 和 encodeURIComponent()。通过对比这些方法对ASCII字符、特殊符号等的处理差异,帮助读者理解何时使用哪种方法更为合适。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法不被编码的ASCII字符
相同字符不同字符总计
escape()0-9
a-z
A-Z
* . _ -
@ + /69
encodeURI()! ( ) ' ~ , : ; # $ @ / & ? + =82
encodeURIComponent()! ( ) ' ~71

字符ASCII
(十六进制)
Unicode
(十六进制)
Unicode
(二进制)
UTF-8存储
(十六进制)
UTF-8字节流
(二进制)
escapeencodeURIencodeURIComponent
A414100000000 010000014101000001AAA
Z5A5A00000000 010110105A01011010ZZZ
a616100000000 011000016101100001aaa
z7A7A00000000 011110107A01111010zzz
0303000000000 001100003000110000000
9393900000000 001110013900111001999
-4E 0001001110 00000000E4 B8 8011100100 10111000 10000000%u4E00%E4%B8%80%E4%B8%80
-9F A510011111 10100101E9 BE A511101001 10111110 10100101%u9FA5%E9%BE%A5%E9%BE%A5

欢乐在不言之中!


测试代码:

运行代码

	var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*._-";

	document.writeln("<br />测试1. 不被编码的字符 —— ASCII字母和数字和 * . _ -");
	document.writeln("<hr />");
	document.writeln("escape(\"" + s + "\") : "  + escape(s) + "<br />");
	document.writeln("encodeURI(\"" + s + "\") : "  + encodeURI(s) + "<br />");
	document.writeln("encodeURIComponent(\"" + s + "\") : "  + encodeURIComponent(s) + "<br />");

	document.writeln("<br />测试2. 不被编码的字符 —— 特殊符号");
	document.writeln("<hr />");

	document.writeln("escape(\"*-+@_./\") : "		 + escape("*@-_+./" ) + "<br />");
	document.writeln("encodeURI(\"!*()'~_.,:;#$@/&?+-=\")  : "	+ encodeURI("!*()'~-_.,:;#$@/&?+=" ) + "<br />");
	document.writeln("encodeURIComponent(\"!*()'~-_.\")  : "	+ encodeURIComponent("!*()'~-_." ) + "<br />");

	document.writeln("<br />测试3. 会被编码的字符 —— 特殊符号");
	document.writeln("<hr />");
	document.writeln("escape(\"?!=()#%&$,:;'[]{}\\`\") : "  + escape("?!=()#%&$,:;'[]{}\\`") + "<br />");
	document.writeln("encodeURI(\"[]{}\\\"%`\")  : "	+ encodeURI("[]{}\\\"%`" ) + "<br />");
	document.writeln("encodeURIComponent(\",:;#$@/&?+=[]{}\\\"%`\")  : " + encodeURIComponent(",:;#$@/&?+=[]{}\\\"%`" ) + "<br />");
	document.writeln("escape(\"然\") : "  + escape("然") + "<br />");
	document.writeln("encodeURI(\"然\") : "  + encodeURI("然") + "<br />");
	document.writeln("encodeURIComponent(\"然\") : "  + encodeURIComponent("然") + "<br />");

运行代码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值