🔗 URL 转码工具

在线 URL 编码/解码 | encodeURIComponent / encodeURI | 实时演示

📝 在线演示

💡 说明

encodeURIComponent() 转义除了字母、数字、( ) . ! ~ * ' - _ 之外的所有字符。适合编码 URL 参数值。

encodeURI() 不会转义 URI 中具有特殊含义的字符:;/?:@&=+$,#。适合编码整个 URL。

解码函数 对应将编码后的字符串还原。

📖 encodeURIComponent() 与 encodeURI() 详解

一、encodeURIComponent()

语法: encodeURIComponent(URIstring)

参数: URIstring,必需。一个字符串,含有 URI 组件或其他要编码的文本。

示例:

document.write(encodeURIComponent("https://www.sojson.com/encodeurl.html?我是个中文参数"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))

结果:
http%3A%2F%2Fwww.sojson.com%2Fencodeurl.html%3F%E6%88%91%E6%98%AF%E4%B8%AA%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0
%3Cbr%20%2F%3E
%2C%2F%3F%3A%40%26%3D%2B%24%23

二、encodeURI()

示例:

document.write(encodeURI("https://www.sojson.com/encodeurl.html?我是个中文参数"))
document.write("<br />")
document.write(encodeURI(",/?:@&=+$#"))

结果:
https://www.sojson.com/encodeurl.html?%E6%88%91%E6%98%AF%E4%B8%AA%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0
%3Cbr%20/%3E
,/?:@&=+$#

三、区别

encodeURIComponent() 假定参数是 URI 的一部分(如查询参数值),会转义 ;/?:@&=+$,# 等分隔符。
encodeURI() 假定参数是完整 URI,不会转义这些保留字符,因此适合编码整个 URL。