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"