decodeURI、decodeURIComponent、encodeURI、encodeURIComponent理解

1、decodeURI() 用于解码使用 encodeURI() 或encodeURIComponent() 编码的字符串。(用于解码已被编码的 URI 字符串,将其还原为原始的字符串形式)

下面是一个简单的示例,展示如何使用 decodeURI() 函数:

const encodedString = 'https://www.example.com/%E4%BD%A0%E5%A5%BD';
const decodedString = decodeURI(encodedString);

console.log(decodedString);
// 输出: "https://www.example.com/你好"

2、decodeURIComponent() 用于解码使用 encodeURIComponent() 编码的 URI 组件(用于解码已被编码的 URI 组件,将其还原为原始的形式)

下面是一个简单的示例,展示如何使用 decodeURIComponent() 函数:

const encodedComponent = '%E4%BD%A0%E5%A5%BD';
const decodedComponent = decodeURIComponent(encodedComponent);

console.log(decodedComponent);
// 输出: "你好"

3、encodeURI()用于对字符串进行 URI(Uniform Resource Identifier)编码。

下面是一个示例,展示如何使用 encodeURI() 函数对字符串进行编码:

const originalString = 'https://www.example.com/你好';
const encodedString = encodeURI(originalString);

console.log(encodedString);
// 输出: "https://www.example.com/%E4%BD%A0%E5%A5%BD"

4、encodeURIComponent() 用于对字符串进行 URI 组件(如查询字符串参数)编码。

下面是一个示例,展示如何使用 encodeURIComponent() 函数对字符串进行编码:

const originalString = '你好,世界!';
const encodedString = encodeURIComponent(originalString);

console.log(encodedString);
// 输出: "%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81"

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值