js的 encodeURI() encodeURIComponent() decodeURI() decodeURIComponent() 笔记250205

js的 encodeURI() encodeURIComponent() decodeURI() decodeURIComponent()

在JavaScript中,处理URI编码和解码的四个关键函数为:encodeURI()encodeURIComponent()decodeURI()decodeURIComponent()。它们分别用于不同的场景,具体区别和用法如下:


1. encodeURI()
作用:编码完整的URI,保留保留字符(如:/?#@等),仅对非保留字符进行编码。

适用场景:编码整个URL,确保URI结构不被破坏。

示例

const url = "http://example.com/path?name=张三&age=20";
const encodedUrl = encodeURI(url);
console.log(encodedUrl);
// 输出: "http://example.com/path?name=%E5%BC%A0%E4%B8%89&age=20"
  • 保留字符://?&等未被编码。
  • 编码字符:中文张三被编码为%E5%BC%A0%E4%B8%89

2. encodeURIComponent()
作用:编码URI的组成部分(如查询参数),对所有非标准字符(包括保留字符)进行编码。

适用场景:编码URI的某部分(如参数值),防止特殊字符破坏URI结构。

示例

const param = "name=张三&city=北京";
const encodedParam = encodeURIComponent(param);
console.log(encodedParam);
// 输出: "name%3D%E5%BC%A0%E4%B8%89%26city%3D%E5%8C%97%E4%BA%AC"
  • 编码保留字符=被编码为%3D&被编码为%26
  • 结果用途:适合作为查询参数的值。

3. decodeURI()
作用:解码由encodeURI()编码的URI,仅还原非保留字符的编码,保留保留字符的编码。

示例

const encodedUrl = "http://example.com/path%20to%25file?q=%25test";
const decodedUrl = decodeURI(encodedUrl);
console.log(decodedUrl);
// 输出: "http://example.com/path to%file?q=%25test"
  • 解码字符%20解码为空格,%25保留为%
  • 保留编码?未被解码,保持原样。

4. decodeURIComponent()
作用:解码由encodeURIComponent()编码的字符串,还原所有字符的编码(包括保留字符)。

示例

const encodedParam = "name%3D%E5%BC%A0%E4%B8%89%26city%3D%E5%8C%97%E4%BA%AC";
const decodedParam = decodeURIComponent(encodedParam);
console.log(decodedParam);
// 输出: "name=张三&city=北京"
  • 解码保留字符%3D解码为=%26解码为&

核心对比表

函数编码/解码范围典型应用场景示例输入 → 输出
encodeURI()保留保留字符,编码非保留字符编码完整URLhttp://ex.com/path?name=张三 → 保留?
encodeURIComponent()编码所有非标准字符(包括保留符)编码URL组件(如参数值)name=张三&age=20name%3D...
decodeURI()仅解码非保留字符的编码解码完整URL%20 → 空格,%25%
decodeURIComponent()解码所有字符的编码解码URL组件(如参数值)%3D=%26&

使用注意事项

  1. 配对使用

    • encodeURI()decodeURI()
    • encodeURIComponent()decodeURIComponent()
    • 混用会导致错误:例如,用decodeURI()解码encodeURIComponent()的结果,可能保留%3D等编码。
  2. 错误处理

    try {decodeURIComponent("%zz"); // 无效编码,抛出URIError
    } catch (e) {console.error("解码失败:", e);
    }
    
  3. 实际应用

    • 构造带参数的URL
      const baseURL = "http://example.com/search";
      const query = encodeURIComponent("q=测试&page=1");
      const finalURL = `${baseURL}?${query}`; // "http://example.com/search?q%3D%E6%B5%8B%E8%AF%95%26page%3D1"
      

总结

  • 编码选择
    • 对整个URL使用encodeURI(),保留结构。
    • 对参数、哈希值等使用encodeURIComponent(),确保特殊字符不破坏URI。
  • 解码选择
    • decodeURI()还原完整URL。
    • decodeURIComponent()还原参数值等部分。

正确使用这些函数,可以避免URI解析错误,确保数据在传输过程中的完整性。









在JavaScript中,encodeURI(), encodeURIComponent(), decodeURI(), 和 decodeURIComponent() 是用于处理URI(统一资源标识符)编码和解码的函数。它们的主要作用是确保URI中的字符能够被正确地传输和解析,特别是在通过浏览器地址栏或Ajax请求发送数据时尤为重要。

  1. encodeURI()

    • 用途:将一个字符串作为完整的URI进行编码。
    • 它不会对以下字符进行编码:字母、数字、以及这些符号 - _ . ! ~ * ' ( )
    • 此外,它也不会对保留字符进行编码,保留字符包括:; , / ? : @ & = + $ #
    • 示例:
      console.log(encodeURI("https://example.com/test?name=John Doe"));
      // 输出: "https://example.com/test?name=John%20Doe"
      
  2. encodeURIComponent()

    • 用途:将一个字符串作为URI组件进行编码。
    • encodeURI()不同的是,encodeURIComponent()会对更多的字符进行编码,包括保留字符如; , / ? : @ & = + $ #等。
    • 这使得它更适合用于编码URL中的查询参数值。
    • 示例:
      console.log(encodeURIComponent("John Doe"));
      // 输出: "John%20Doe"
      console.log(encodeURIComponent("?name=John Doe"));
      // 输出: "%3Fname%3DJohn%20Doe"
      
  3. decodeURI()

    • 用途:对使用encodeURI()编码的字符串进行解码。
    • 示例:
      console.log(decodeURI("https://example.com/test?name=John%20Doe"));
      // 输出: "https://example.com/test?name=John Doe"
      
  4. decodeURIComponent()

    • 用途:对使用encodeURIComponent()编码的字符串进行解码。
    • 示例:
      console.log(decodeURIComponent("John%20Doe"));
      // 输出: "John Doe"
      console.log(decodeURIComponent("%3Fname%3DJohn%20Doe"));
      // 输出: "?name=John Doe"
      

选择使用哪一个函数取决于你的具体需求。如果你需要对整个URL进行编码或解码,应该使用encodeURI()decodeURI()。而当你只需要对URL的一部分(比如查询参数)进行编码或解码时,则应使用encodeURIComponent()decodeURIComponent()









在JavaScript中,处理URL时经常会遇到需要对URL的某些部分进行编码和解码的情况。JavaScript提供了四个内置函数来处理这些需求:encodeURI(), encodeURIComponent(), decodeURI(), 和 decodeURIComponent()。以下是这些函数的详细解释和用法:

1. encodeURI()

encodeURI() 函数用于对完整的URI(统一资源标识符)进行编码。它不会对URI中的特殊字符(如冒号:、斜杠/、问号?、井号#等)进行编码,因为这些字符在URI中有特定的含义。

用法示例

let uri = "https://example.com/path?name=John Doe&age=30";
let encodedUri = encodeURI(uri);
console.log(encodedUri); // 输出: "https://example.com/path?name=John Doe&age=30"

在这个例子中,由于URI中的字符都不需要编码,所以输出和输入相同。

2. encodeURIComponent()

encodeURIComponent() 函数用于对URI的组件进行编码,通常用于编码URI中的查询字符串参数。这个函数会对大多数非字母数字字符进行编码。

用法示例

let componentName = "John Doe";
let encodedComponentName = encodeURIComponent(componentName);
console.log(encodedComponentName); // 输出: "John%20Doe"

在这个例子中,空格被编码为%20

3. decodeURI()

decodeURI() 函数用于对使用encodeURI()编码的完整URI进行解码。

用法示例

let encodedUri = "https%3A%2F%2Fexample.com%2Fpath%3Fname%3DJohn%20Doe%26age%3D30";
let decodedUri = decodeURI(encodedUri);
console.log(decodedUri); // 输出: "https://example.com/path?name=John Doe&age=30"

注意,这个例子中,我们假设有一个错误的URI编码(实际上这样的编码通常不会由encodeURI()产生,这里只是为了演示),decodeURI()会将其解码回原始形式。

4. decodeURIComponent()

decodeURIComponent() 函数用于对使用encodeURIComponent()编码的URI组件进行解码。

用法示例

let encodedComponentName = "John%20Doe";
let decodedComponentName = decodeURIComponent(encodedComponentName);
console.log(decodedComponentName); // 输出: "John Doe"

在这个例子中,%20被解码回空格。

总结

  • encodeURI()decodeURI() 用于完整的URI。
  • encodeURIComponent()decodeURIComponent() 用于URI的组件,特别是查询字符串参数。
  • 使用这些函数可以确保URL在传输过程中不会因为特殊字符而出现问题,同时保证URL的可读性和功能性。









在 JavaScript 中,encodeURI()encodeURIComponent()decodeURI()decodeURIComponent() 是用于处理 URI(统一资源标识符)编码和解码的四个重要函数,下面将分别详细介绍它们的功能和使用场景。

1. encodeURI()

  • 功能encodeURI() 函数用于对整个 URI 进行编码。它会将 URI 中具有特殊含义的字符(如 ;,/?:@&=+$ 等)保持不变,只对那些非标准的字符(如中文、特殊符号等)进行编码。
  • 语法
encodeURI(uri);
  • 参数uri 是要编码的完整 URI 字符串。
  • 示例
const uri = "https://example.com/path with spaces/index.html?param=值";
const encodedUri = encodeURI(uri);
console.log(encodedUri); 
// 输出: https://example.com/path%20with%20spaces/index.html?param=%E5%80%BC

2. encodeURIComponent()

  • 功能encodeURIComponent() 函数用于对 URI 组件(如查询参数的值、路径段等)进行编码。它会将除了字母、数字、-_.!~*'() 之外的所有字符都进行编码。
  • 语法
encodeURIComponent(uriComponent);
  • 参数uriComponent 是要编码的 URI 组件字符串。
  • 示例
const component = "value with spaces & special characters";
const encodedComponent = encodeURIComponent(component);
console.log(encodedComponent); 
// 输出: value%20with%20spaces%20%26%20special%20characters

3. decodeURI()

  • 功能decodeURI() 函数用于对使用 encodeURI() 编码后的 URI 进行解码,将编码后的字符还原为原始字符。
  • 语法
decodeURI(encodedURI);
  • 参数encodedURI 是要解码的经过 encodeURI() 编码的 URI 字符串。
  • 示例
const encodedUri = "https://example.com/path%20with%20spaces/index.html?param=%E5%80%BC";
const decodedUri = decodeURI(encodedUri);
console.log(decodedUri); 
// 输出: https://example.com/path with spaces/index.html?param=值

4. decodeURIComponent()

  • 功能decodeURIComponent() 函数用于对使用 encodeURIComponent() 编码后的 URI 组件进行解码,将编码后的字符还原为原始字符。
  • 语法
decodeURIComponent(encodedURIComponent);
  • 参数encodedURIComponent 是要解码的经过 encodeURIComponent() 编码的 URI 组件字符串。
  • 示例
const encodedComponent = "value%20with%20spaces%20%26%20special%20characters";
const decodedComponent = decodeURIComponent(encodedComponent);
console.log(decodedComponent); 
// 输出: value with spaces & special characters

使用场景总结

  • encodeURI()decodeURI():当你需要对整个 URI 进行编码或解码时,使用这两个函数。例如,在构建一个完整的 URL 时,如果 URL 中包含非标准字符,可以使用 encodeURI() 进行编码。
  • encodeURIComponent()decodeURIComponent():当你需要对 URI 中的某个组件(如查询参数的值)进行编码或解码时,使用这两个函数。例如,在处理表单数据时,将用户输入的值作为查询参数添加到 URL 中,需要使用 encodeURIComponent() 对值进行编码。









计划

  1. 解释 encodeURI()encodeURIComponent() 的区别。
  2. 解释 decodeURI()decodeURIComponent() 的区别。
  3. 提供示例代码展示这些函数的使用。

代码

// encodeURI() 和 encodeURIComponent() 的区别
// encodeURI() 不会编码 ASCII 字母和数字以及部分特殊字符,适用于编码整个 URI
// encodeURIComponent() 会编码所有非标准字符,适用于编码 URI 的组成部分(如查询参数)let uri = "https://example.com/搜索?query=测试";
let encodedURI = encodeURI(uri);
let encodedURIComponent = encodeURIComponent(uri);console.log("encodeURI:", encodedURI); // 输出: https://example.com/%E6%90%9C%E7%B4%A2?query=%E6%B5%8B%E8%AF%95
console.log("encodeURIComponent:", encodedURIComponent); // 输出: https%3A%2F%2Fexample.com%2F%E6%90%9C%E7%B4%A2%3Fquery%3D%E6%B5%8B%E8%AF%95// decodeURI() 和 decodeURIComponent() 的区别
// decodeURI() 用于解码由 encodeURI() 编码的字符串
// decodeURIComponent() 用于解码由 encodeURIComponent() 编码的字符串let decodedURI = decodeURI(encodedURI);
let decodedURIComponent = decodeURIComponent(encodedURIComponent);console.log("decodeURI:", decodedURI); // 输出: https://example.com/搜索?query=测试
console.log("decodeURIComponent:", decodedURIComponent); // 输出: https://example.com/搜索?query=测试

解释

  • encodeURI()decodeURI() 主要用于处理整个 URI。
  • encodeURIComponent()decodeURIComponent() 主要用于处理 URI 的组成部分,如查询参数。
















encodeURI()和encodeURIComponent()有什么区别?

在JavaScript中,处理URI编码和解码的四个关键函数为:encodeURI()encodeURIComponent()decodeURI()decodeURIComponent()。它们分别用于不同的场景,具体区别和用法如下:


1. encodeURI()
作用:编码完整的URI,保留保留字符(如:/?#@等),仅对非保留字符进行编码。

适用场景:编码整个URL,确保URI结构不被破坏。

示例

const url = "http://example.com/path?name=张三&age=20";
const encodedUrl = encodeURI(url);
console.log(encodedUrl);
// 输出: "http://example.com/path?name=%E5%BC%A0%E4%B8%89&age=20"
  • 保留字符://?&等未被编码。
  • 编码字符:中文张三被编码为%E5%BC%A0%E4%B8%89

2. encodeURIComponent()
作用:编码URI的组成部分(如查询参数),对所有非标准字符(包括保留字符)进行编码。

适用场景:编码URI的某部分(如参数值),防止特殊字符破坏URI结构。

示例

const param = "name=张三&city=北京";
const encodedParam = encodeURIComponent(param);
console.log(encodedParam);
// 输出: "name%3D%E5%BC%A0%E4%B8%89%26city%3D%E5%8C%97%E4%BA%AC"
  • 编码保留字符=被编码为%3D&被编码为%26
  • 结果用途:适合作为查询参数的值。

3. decodeURI()
作用:解码由encodeURI()编码的URI,仅还原非保留字符的编码,保留保留字符的编码。

示例

const encodedUrl = "http://example.com/path%20to%25file?q=%25test";
const decodedUrl = decodeURI(encodedUrl);
console.log(decodedUrl);
// 输出: "http://example.com/path to%file?q=%25test"
  • 解码字符%20解码为空格,%25保留为%
  • 保留编码?未被解码,保持原样。

4. decodeURIComponent()
作用:解码由encodeURIComponent()编码的字符串,还原所有字符的编码(包括保留字符)。

示例

const encodedParam = "name%3D%E5%BC%A0%E4%B8%89%26city%3D%E5%8C%97%E4%BA%AC";
const decodedParam = decodeURIComponent(encodedParam);
console.log(decodedParam);
// 输出: "name=张三&city=北京"
  • 解码保留字符%3D解码为=%26解码为&

核心对比表

函数编码/解码范围典型应用场景示例输入 → 输出
encodeURI()保留保留字符,编码非保留字符编码完整URLhttp://ex.com/path?name=张三 → 保留?
encodeURIComponent()编码所有非标准字符(包括保留符)编码URL组件(如参数值)name=张三&age=20name%3D...
decodeURI()仅解码非保留字符的编码解码完整URL%20 → 空格,%25%
decodeURIComponent()解码所有字符的编码解码URL组件(如参数值)%3D=%26&

使用注意事项

  1. 配对使用

    • encodeURI()decodeURI()
    • encodeURIComponent()decodeURIComponent()
    • 混用会导致错误:例如,用decodeURI()解码encodeURIComponent()的结果,可能保留%3D等编码。
  2. 错误处理

    try {decodeURIComponent("%zz"); // 无效编码,抛出URIError
    } catch (e) {console.error("解码失败:", e);
    }
    
  3. 实际应用

    • 构造带参数的URL
      const baseURL = "http://example.com/search";
      const query = encodeURIComponent("q=测试&page=1");
      const finalURL = `${baseURL}?${query}`; // "http://example.com/search?q%3D%E6%B5%8B%E8%AF%95%26page%3D1"
      

总结

  • 编码选择
    • 对整个URL使用encodeURI(),保留结构。
    • 对参数、哈希值等使用encodeURIComponent(),确保特殊字符不破坏URI。
  • 解码选择
    • decodeURI()还原完整URL。
    • decodeURIComponent()还原参数值等部分。

正确使用这些函数,可以避免URI解析错误,确保数据在传输过程中的完整性。









encodeURI()encodeURIComponent() 都是 JavaScript 中用于对 URI(统一资源标识符)进行编码的函数,但它们在处理字符的方式和适用场景上有所不同。以下是它们之间的主要区别:

encodeURI()

  • 用途encodeURI() 适用于对整个 URI 进行编码。

  • 编码范围:它不会对以下字符进行编码:

    • 字母、数字
    • 特殊字符 - _ . ! ~ * ' ( )
    • 保留字符 ; , / ? : @ & = + $ #

    因此,encodeURI() 主要用于那些需要将特殊字符保留在 URI 中的情况,例如当您需要保持 URL 的结构完整性时。

  • 示例

    console.log(encodeURI("https://example.com/test?name=John Doe&age=30"));
    // 输出: "https://example.com/test?name=John%20Doe&age=30"
    

encodeURIComponent()

  • 用途encodeURIComponent() 更加严格,适用于对 URI 的组件(如查询字符串中的参数名或值)进行编码。

  • 编码范围:除了字母、数字和特殊字符 - _ . ! ~ * ' ( ) 外,encodeURIComponent() 还会对保留字符 ; , / ? : @ & = + $ # 进行编码。

    这意味着它可以确保 URI 组件不会破坏整个 URI 的结构,特别适合用来构建查询字符串或其他 URI 组件。

  • 示例

    console.log(encodeURIComponent("John Doe"));
    // 输出: "John%20Doe"
    console.log(encodeURIComponent("https://example.com/test?name=John Doe&age=30"));
    // 输出: "https%3A%2F%2Fexample.com%2Ftest%3Fname%3DJohn%20Doe%26age%3D30"
    

区别总结

  • 当你想要对一个完整的 URI 编码,同时保留其原有的结构和特殊字符时,使用 encodeURI()
  • 如果你需要对 URI 的某个部分(比如查询参数或者片段标识符)进行编码,以避免这些部分影响到整个 URI 的解析,则应该使用 encodeURIComponent()

选择合适的函数取决于你正在处理的是整个 URI 还是 URI 的一部分。正确地使用这两个函数可以帮助你在网络传输过程中准确无误地传递数据。









encodeURI()encodeURIComponent()都是JavaScript中用于对URI(统一资源标识符)进行编码的函数,但它们之间存在一些关键的区别:

主要区别

  1. 编码范围

    • encodeURI():主要用于对整个URI进行编码。它不会编码URI中的特殊字符,如冒号(:)、斜杠(/)、问号(?)、井号(#)、中括号([])等,因为这些字符在URI中有特定的含义。
    • encodeURIComponent():主要用于对URI中的某一部分(通常是查询字符串参数)进行编码。它会编码大多数非字母数字字符,包括URI中的特殊字符(如上所述的冒号、斜杠等,在查询字符串参数的上下文中会被编码)。
  2. 使用场景

    • encodeURI():适用于对整个URL进行编码,当你需要确保整个URL在传输过程中不会因为特殊字符而出现问题时使用。
    • encodeURIComponent():适用于对URL的查询字符串参数进行编码,当你需要将包含特殊字符的参数值嵌入到URL中时使用。

示例对比

假设我们有一个URL和一个查询字符串参数:

let baseUrl = "https://example.com/search?";
let queryParam = "name=John Doe&age=30";
  • 使用encodeURI()对整个URL进行编码(通常不需要,因为URL本身已经是一个有效的URI):
let encodedUri = encodeURI(baseUrl + queryParam);
console.log(encodedUri);
// 输出: "https://example.com/search?name=John Doe&age=30"
// 注意:这里的空格没有被编码,因为空格在URI的路径部分(不是查询字符串)中通常不需要编码。
// 但在这个例子中,由于我们是在模拟整个URL的编码,而整个URL已经是有效的,所以编码结果没有变化。
  • 使用encodeURIComponent()对查询字符串参数进行编码:
let encodedQueryParam = encodeURIComponent(queryParam);
console.log(encodedQueryParam);
// 输出: "name%3DJohn%20Doe%26age%3D30"
// 注意:这里的空格被编码为%20,特殊字符&被编码为%26。

然而,在实际应用中,我们通常不会对整个查询字符串使用encodeURIComponent()进行编码,而是分别对每个参数值进行编码,然后将它们拼接起来形成完整的查询字符串。例如:

let name = "John Doe";
let age = 30;
let encodedName = encodeURIComponent(name);
let encodedAge = encodeURIComponent(age.toString());
let fullQueryParam = `name=${encodedName}&age=${encodedAge}`;
console.log(fullQueryParam);
// 输出: "name=John%20Doe&age=30"

结论

  • 使用encodeURI()对整个URI进行编码时,它会保留URI中的特殊字符。
  • 使用encodeURIComponent()对URI的某一部分(如查询字符串参数)进行编码时,它会编码大多数非字母数字字符。

在实际开发中,根据具体需求选择合适的函数进行编码是非常重要的。









encodeURI()encodeURIComponent() 都是 JavaScript 中用于对 URI(统一资源标识符)进行编码的函数,但它们在功能和使用场景上存在一些区别,下面为你详细介绍:

1. 编码范围不同

  • encodeURI():该函数主要用于对整个 URI 进行编码。它会保留 URI 中具有特殊含义的字符,这些字符包括 ;,/?:@&=+$ 等,因为这些字符在 URI 中有特定的用途,不应该被编码。它只对那些非标准的字符(如中文、特殊符号等)进行编码。
const uri = "https://example.com/path with spaces/index.html?param=值";
const encodedUri = encodeURI(uri);
console.log(encodedUri); 
// 输出: https://example.com/path%20with%20spaces/index.html?param=%E5%80%BC

在上述示例中,URI 中的特殊字符 :/? 等都没有被编码,只有空格和中文字符被编码了。

  • encodeURIComponent():此函数用于对 URI 组件(如查询参数的值、路径段等)进行编码。它会将除了字母、数字以及 -_.!~*'() 之外的所有字符都进行编码。
const component = "value with spaces & special characters";
const encodedComponent = encodeURIComponent(component);
console.log(encodedComponent); 
// 输出: value%20with%20spaces%20%26%20special%20characters

在这个示例中,空格、& 等字符都被编码了。

2. 使用场景不同

  • encodeURI():当你需要对整个 URI 进行编码,并且希望保留 URI 中特殊字符的原始含义时,使用 encodeURI()。例如,在构建一个完整的 URL 时,如果 URL 中包含非标准字符,可以使用 encodeURI() 进行编码。
const baseUrl = "https://example.com/search";
const query = "javascript 教程";
const fullUrl = `${baseUrl}?q=${query}`;
const encodedFullUrl = encodeURI(fullUrl);
console.log(encodedFullUrl); 
// 输出: https://example.com/search?q=javascript%20%E6%95%99%E7%A8%8B
  • encodeURIComponent():当你需要对 URI 中的某个组件(如查询参数的值、路径段等)进行编码时,使用 encodeURIComponent()。例如,在处理表单数据时,将用户输入的值作为查询参数添加到 URL 中,需要使用 encodeURIComponent() 对值进行编码,以确保特殊字符不会破坏 URL 的结构。
const paramName = "q";
const paramValue = "javascript & tutorial";
const encodedParamValue = encodeURIComponent(paramValue);
const queryString = `${paramName}=${encodedParamValue}`;
console.log(queryString); 
// 输出: q=javascript%20%26%20tutorial

综上所述,encodeURI() 适用于编码整个 URI,而 encodeURIComponent() 适用于编码 URI 中的组件。
















decodeURI()和decodeURIComponent()有什么区别?

decodeURI()decodeURIComponent() 是 JavaScript 中用于解码 URL 编码字符的两个关键函数,它们的核心区别在于解码范围使用场景。以下是详细对比:


一、核心区别

特性decodeURI()decodeURIComponent()
解码范围解码完整 URI,但保留保留字符(如 :/?#)的编码解码所有编码字符,包括保留字符(如 :/?#
用途处理整个 URI(如 http://example.com/path处理 URI 的某一部分(如查询参数、哈希值)
对应编码函数encodeURI()encodeURIComponent()
保留字符不解码 ;/?:@&=+$,# 等保留字符的编码解码所有字符的编码(包括保留字符)

二、具体行为对比
1. 对保留字符的处理

  • decodeURI():不解码保留字符的编码。
    例如:%3F? 的编码)不会被解码,仍保持 %3F

    const encodedURI = "http://example.com/%3Fquery%3Dtest";
    console.log(decodeURI(encodedURI)); 
    // 输出: "http://example.com/%3Fquery=test" (%3F 未被解码)
    
  • decodeURIComponent():解码所有保留字符的编码。
    例如:%3F 会被解码为 ?

    const encodedComponent = "%3Fquery%3Dtest";
    console.log(decodeURIComponent(encodedComponent)); 
    // 输出: "?query=test" (%3F 被解码为 ?)
    

2. 对非保留字符的处理
两者都会解码非保留字符的编码(如空格 %20 解码为空格):

const encodedSpace = "Hello%20World%2525";
console.log(decodeURI(encodedSpace));        // 输出: "Hello World%25"
console.log(decodeURIComponent(encodedSpace)); // 输出: "Hello World%"
  • %20 → 空格(两者均解码)。
  • %2525%25decodeURI() 解码一次)→ %decodeURIComponent() 解码两次)。

3. 错误处理

  • 两者在遇到无效编码(如 %zz)时均会抛出 URIError
    try {decodeURI("http://example.com/%zz"); // 抛出 URIError
    } catch (e) {console.error(e); 
    }
    

三、使用场景
1. 何时用 decodeURI()

  • 解码完整的 URL:保留 URL 结构(如协议、路径分隔符)。
    const encodedURL = "http://example.com/%E6%B5%8B%E8%AF%95?q=%25value";
    console.log(decodeURI(encodedURL)); 
    // 输出: "http://example.com/测试?q=%25value" (保留 ? 和 %25)
    

2. 何时用 decodeURIComponent()

  • 解码 URI 的组成部分:如查询参数、哈希值。
    const encodedParam = "q%3D%25E6%B5%8B%E8%AF%95";
    console.log(decodeURIComponent(encodedParam)); 
    // 输出: "q=%E6%B5%8B%E8%AF%95" (解码 ? 的编码 %3D 为 =,%25 为 %)
    

四、编码与解码的配对规则

操作正确配对函数错误示例
编码完整 URIencodeURI()decodeURI()encodeURIComponent() + decodeURI() → 结构破坏
编码 URI 组件encodeURIComponent()decodeURIComponent()encodeURI() + decodeURIComponent() → 保留字符被解码

五、实际示例
场景 1:解码整个 URL

const encodedURL = "http://example.com/%E4%BD%A0%E5%A5%BD%3Fq%3D%25test";
const decodedURL = decodeURI(encodedURL);
console.log(decodedURL); 
// 输出: "http://example.com/你好?q=%25test" (保留 ? 和 %25)

场景 2:解码查询参数

const encodedParam = "q%3D%25E4%BD%A0%E5%A5%BD";
const decodedParam = decodeURIComponent(encodedParam);
console.log(decodedParam); 
// 输出: "q=%你好" (解码 %3D 为 =,%25 为 %)

六、总结

函数使用场景关键行为
decodeURI()处理完整 URI保留保留字符的编码(如 :/?#
decodeURIComponent()处理 URI 组件(如参数、哈希)解码所有字符的编码(包括保留字符)

选择正确的解码函数可避免 URL 结构破坏或数据解析错误。









decodeURI()decodeURIComponent()都是JavaScript中用于对URI(统一资源标识符)进行解码的函数,但它们之间存在一些关键的区别:

主要区别

  1. 解码范围

    • decodeURI():主要用于对整个URI进行解码。它主要解码由encodeURI()函数编码的URI,且不会解码那些原本在URI中就具有特殊含义的字符(如冒号:、斜杠/、问号?、井号#等)。
    • decodeURIComponent():主要用于对URI的组件(通常是查询字符串参数)进行解码。它解码由encodeURIComponent()函数编码的URI组件,并且会解码大多数之前被编码的字符。
  2. 使用场景

    • decodeURI():当你需要对一个完整的、已经编码的URI进行解码时使用。
    • decodeURIComponent():当你需要从URI中提取并解码查询字符串参数时使用。

示例对比

假设我们有一个已经编码的URI和一个编码的URI组件:

let encodedURI = "https%3A%2F%2Fexample.com%2Fpath%3Fname%3DJohn%2520Doe";
let encodedURIComponent = "John%2520Doe"; // 这里的%2520实际上是%20(空格)被再次编码的结果
  • 使用decodeURI()对整个URI进行解码:
let decodedURI = decodeURI(encodedURI);
console.log(decodedURI);
// 输出: "https://example.com/path?name=John%20Doe"
// 注意:这里的%20(空格)没有被进一步解码,因为它在URI的查询字符串部分是有效的。
  • 使用decodeURIComponent()对URI组件进行解码:
let decodedURIComponent = decodeURIComponent(encodeURIComponent);
console.log(decodedURIComponent);
// 输出: "John Doe"
// 注意:这里的%2520被解码为%20,然后%20再被解码为空格。
// 但在实际应用中,我们通常会直接对编码后的查询字符串参数使用decodeURIComponent(),
// 而不会遇到像%2520这样的双重编码情况。
// 更常见的示例可能是:
let directEncodedComponent = "John%20Doe";
let directDecodedComponent = decodeURIComponent(directEncodedComponent);
console.log(directDecodedComponent);
// 输出: "John Doe"

注意事项

  • 在实际应用中,我们通常使用decodeURIComponent()来解码查询字符串参数,因为这些参数在嵌入到URI之前通常会被encodeURIComponent()编码。
  • 使用decodeURI()时,请确保URI是有效的,并且是由encodeURI()或类似函数编码的,以避免解码错误。
  • 不要对已经解码的URI或URI组件再次进行解码,这可能会导致数据损坏或不可预测的行为。

结论

  • 使用decodeURI()对整个URI进行解码时,它会保留URI中的特殊字符的编码形式(如果这些字符在URI中有特殊含义)。
  • 使用decodeURIComponent()对URI的组件进行解码时,它会解码大多数之前被编码的字符,包括空格和其他特殊字符。

在实际开发中,根据具体需求选择合适的函数进行解码是非常重要的。









decodeURI()decodeURIComponent() 都是 JavaScript 中用于解码 URI(统一资源标识符)的函数,但它们在处理编码字符的方式和适用场景上有所不同。以下是它们之间的主要区别:

decodeURI()

  • 用途decodeURI() 用于对使用 encodeURI() 编码过的整个 URI 进行解码。
  • 解码范围:它只会解码那些由 encodeURI() 编码的字符,即除了字母、数字、保留字符(; , / ? : @ & = + $ #)以及特殊字符(- _ . ! ~ * ' ( ))以外的所有字符 。
  • 限制:由于 decodeURI() 不会解码保留字符,这意味着如果 URI 包含这些字符的百分比编码形式(例如 %2F 表示 /),那么 decodeURI() 将不会将其转换回原始字符。

decodeURIComponent()

  • 用途decodeURIComponent() 用于对使用 encodeURIComponent() 编码过的 URI 组件进行解码。
  • 解码范围:与 decodeURI() 相比,decodeURIComponent() 可以解码所有通过百分比编码的字符,包括保留字符 。
  • 适用场景:当你需要对 URI 的某个部分(如查询字符串参数或路径片段)进行解码时,应该使用 decodeURIComponent()。这是因为 URI 组件可能包含需要被正确解释为普通文本的保留字符。

示例对比
假设我们有如下编码后的 URI:

let encodedUri = encodeURI("https://example.com/test?name=John Doe&age=30");
// 输出: "https://example.com/test?name=John%20Doe&age=30"let encodedComponent = encodeURIComponent("John Doe");
// 输出: "John%20Doe"

现在我们可以用相应的解码函数来解码它们:

console.log(decodeURI(encodedUri));
// 输出: "https://example.com/test?name=John Doe&age=30"console.log(decodeURIComponent(encodedComponent));
// 输出: "John Doe"

总结

  • 如果你需要解码整个 URI 并且希望保留其中的保留字符不被改变,你应该使用 decodeURI()
  • 当你需要解码 URI 的特定组件,并且该组件可能包含需要被解码的保留字符时,你应该使用 decodeURIComponent()

理解这两个函数的区别对于正确处理 URL 参数和其他 URI 相关的数据至关重要。例如,在构建动态网页或处理 AJAX 请求时,确保正确地编码和解码数据可以避免潜在的安全问题和错误 。









decodeURI()decodeURIComponent() 是 JavaScript 中用于对经过编码的 URI(统一资源标识符)进行解码的两个函数,它们的主要区别与 encodeURI()encodeURIComponent() 相对应,具体如下:

1. 解码范围不同

  • decodeURI():用于对使用 encodeURI() 编码后的整个 URI 进行解码。它只会对那些通过 encodeURI() 编码的非标准字符(如中文、特殊符号等)进行解码,而对于在 encodeURI() 中保留的具有特殊含义的字符(像 ;,/?:@&=+$ 等)不会进行额外处理,因为这些字符在 URI 中有其特定用途,在编码时就未被编码。
const encodedUri = "https://example.com/path%20with%20spaces/index.html?param=%E5%80%BC";
const decodedUri = decodeURI(encodedUri);
console.log(decodedUri); 
// 输出: https://example.com/path with spaces/index.html?param=值

在上述例子中,空格和中文字符被成功解码,而 :/? 等特殊字符保持不变。

  • decodeURIComponent():用于对使用 encodeURIComponent() 编码后的 URI 组件进行解码。它会将所有经过 encodeURIComponent() 编码的字符(除了字母、数字以及 -_.!~*'() 这些在编码时就保留的字符)都还原为原始字符。
const encodedComponent = "value%20with%20spaces%20%26%20special%20characters";
const decodedComponent = decodeURIComponent(encodedComponent);
console.log(decodedComponent); 
// 输出: value with spaces & special characters

这里,编码后的空格、& 等字符都被解码回原始形式。

2. 使用场景不同

  • decodeURI():当你需要对一个完整的、使用 encodeURI() 编码过的 URI 进行还原时,就使用 decodeURI()。例如,在从浏览器地址栏获取完整的 URL 后,如果其中包含编码的部分,就可以用 decodeURI() 来得到原始的 URL。
const fullEncodedUrl = "https://example.com/search?q=javascript%20%E6%95%99%E7%A8%8B";
const fullDecodedUrl = decodeURI(fullEncodedUrl);
console.log(fullDecodedUrl); 
// 输出: https://example.com/search?q=javascript 教程
  • decodeURIComponent():在处理从 URI 中提取出来的某个组件(如查询参数的值)时,若该组件是使用 encodeURIComponent() 编码的,就需要用 decodeURIComponent() 来解码。比如在解析 URL 的查询字符串时,对每个参数的值进行解码。
const queryString = "q=javascript%20%26%20tutorial";
const [paramName, encodedParamValue] = queryString.split('=');
const decodedParamValue = decodeURIComponent(encodedParamValue);
console.log(decodedParamValue); 
// 输出: javascript & tutorial

总之,decodeURI() 针对完整 URI 的解码,decodeURIComponent() 则用于 URI 组件的解码,它们的使用场景取决于编码时采用的是哪种编码函数。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/69128.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Math Reference Notes: 符号函数

1. 符号函数的定义 符号函数(Sign Function) sgn ( x ) \text{sgn}(x) sgn(x) 是一个将实数 ( x ) 映射为其 符号值(即正数、负数或零)的函数。 它的定义如下: sgn ( x ) { 1 如果 x > 0 0 如果 x 0 − 1 如…

一文了解边缘计算

什么是边缘计算? 我们可以通过一个最简单的例子来理解它,它就像一个司令员,身在离炮火最近的前线,汇集现场所有的实时信息,经过分析并做出决策,及时果断而不拖延。 1.什么是边缘计算? 边缘计算…

108,【8】 buuctf web [网鼎杯 2020 青龙组]AreUSerialz

进入靶场 <?php // 包含 flag.php 文件&#xff0c;通常这个文件可能包含敏感信息&#xff0c;如 flag include("flag.php");// 高亮显示当前文件的源代码&#xff0c;方便查看代码结构和逻辑 highlight_file(__FILE__);// 定义一个名为 FileHandler 的类&#x…

《redis哨兵机制》

【redis哨兵机制导读】上一节介绍了redis主从同步的机制&#xff0c;但大家有没有想过一种场景&#xff0c;比如&#xff1a;主库突然挂了&#xff0c;那么按照读写分离的设计思想&#xff0c;此时redis集群只有从库才能提供读服务&#xff0c;那么写服务该如何提供&#xff0c…

【赵渝强老师】Spark RDD的依赖关系和任务阶段

Spark RDD彼此之间会存在一定的依赖关系。依赖关系有两种不同的类型&#xff1a;窄依赖和宽依赖。 窄依赖&#xff1a;如果父RDD的每一个分区最多只被一个子RDD的分区使用&#xff0c;这样的依赖关系就是窄依赖&#xff1b;宽依赖&#xff1a;如果父RDD的每一个分区被多个子RD…

开源数据分析工具 RapidMiner

RapidMiner是一款功能强大且广泛应用的数据分析工具&#xff0c;其核心功能和特点使其成为数据科学家、商业分析师和预测建模人员的首选工具。以下是对RapidMiner的深度介绍&#xff1a; 1. 概述 RapidMiner是一款开源且全面的端到端数据科学平台&#xff0c;支持从数据准备、…

蓝桥杯备考:二维前缀和算法模板题(二维前缀和详解)

【模板】二维前缀和 这道题如果我们暴力求解的话&#xff0c;时间复杂度就是q次查询里套两层循环最差的时候要遍历整个矩阵也就是O&#xff08;q*n*m) 由题目就是10的11次方&#xff0c;超时 二维前缀和求和的公式&#xff08;创建需要用到&#xff09;f[i][j]就是从&#xf…

3-track_hacker/2018网鼎杯

3-track_hacker 打开附件 使用Wireshark打开。过滤器过滤http,看里面有没有flag.txt 发现有 得到&#xff1a;eJxLy0lMrw6NTzPMS4n3TVWsBQAz4wXi base64解密 import base64 import zlibc eJxLy0lMrw6NTzPMS4n3TVWsBQAz4wXi decoded base64.b64decode(c) result zlib.deco…

第二十章 存储函数

目录 一、概述 二、语法 三、示例 一、概述 前面章节中&#xff0c;我们详细讲解了MySQL中的存储过程&#xff0c;掌握了存储过程之后&#xff0c;学习存储函数则肥仓简单&#xff0c;存储函数其实是一种特殊的存储过程&#xff0c;也就是有返回值的存储过程。存储函数的参数…

Linux:文件系统(软硬链接)

目录 inode ext2文件系统 Block Group 超级块&#xff08;Super Block&#xff09; GDT&#xff08;Group Descriptor Table&#xff09; 块位图&#xff08;Block Bitmap&#xff09; inode位图&#xff08;Inode Bitmap&#xff09; i节点表&#xff08;inode Tabl…

java求职学习day27

数据库连接池 &DBUtils 1.数据库连接池 1.1 连接池介绍 1) 什么是连接池 实际开发中 “ 获得连接 ” 或 “ 释放资源 ” 是非常消耗系统资源的两个过程&#xff0c;为了解决此类性能问题&#xff0c;通常情况我们 采用连接池技术&#xff0c;来共享连接 Connection 。…

机器学习--2.多元线性回归

多元线性回归 1、基本概念 1.1、连续值 1.2、离散值 1.3、简单线性回归 1.4、最优解 1.5、多元线性回归 2、正规方程 2.1、最小二乘法 2.2、多元一次方程举例 2.3、矩阵转置公式与求导公式 2.4、推导正规方程0的解 2.5、凸函数判定 成年人最大的自律就是&#xff1a…

Docker 部署 ClickHouse 教程

Docker 部署 ClickHouse 教程 背景 ClickHouse 是一个开源的列式数据库管理系统&#xff08;DBMS&#xff09;&#xff0c;主要用于在线分析处理&#xff08;OLAP&#xff09;。它专为大数据的实时分析设计&#xff0c;支持高速的查询性能和高吞吐量。ClickHouse 以其高效的数…

建表注意事项(2):表约束,主键自增,序列[oracle]

没有明确写明数据库时,默认基于oracle 约束的分类 用于确保数据的完整性和一致性。约束可以分为 表级约束 和 列级约束&#xff0c;区别在于定义的位置和作用范围 复合主键约束: 主键约束中有2个或以上的字段 复合主键的列顺序会影响索引的使用&#xff0c;需谨慎设计 添加…

Google C++ Style / 谷歌C++开源风格

文章目录 前言1. 头文件1.1 自给自足的头文件1.2 #define 防护符1.3 导入你的依赖1.4 前向声明1.5 内联函数1.6 #include 的路径及顺序 2. 作用域2.1 命名空间2.2 内部链接2.3 非成员函数、静态成员函数和全局函数2.4 局部变量2.5 静态和全局变量2.6 thread_local 变量 3. 类3.…

【HTML入门】Sublime Text 4与 Phpstorm

文章目录 前言一、环境基础1.Sublime Text 42.Phpstorm(1)安装(2)启动Phpstorm(3)“启动”码 二、HTML1.HTML简介(1)什么是HTML(2)HTML版本及历史(3)HTML基本结构 2.HTML简单语法(1)HTML标签语法(2)HTML常用标签(3)表格(4)特殊字符 总结 前言 在当今的软件开发领域&#xff0c…

【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.20 傅里叶变换:从时域到频域的算法实现

2.20 傅里叶变换&#xff1a;从时域到频域的算法实现 目录 #mermaid-svg-zrRqIme9IEqP6JJE {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-zrRqIme9IEqP6JJE .error-icon{fill:#552222;}#mermaid-svg-zrRqIme9IEqP…

刷题记录 动态规划-7: 63. 不同路径 II

题目&#xff1a;63. 不同路径 II 难度&#xff1a;中等 给定一个 m x n 的整数数组 grid。一个机器人初始位于 左上角&#xff08;即 grid[0][0]&#xff09;。机器人尝试移动到 右下角&#xff08;即 grid[m - 1][n - 1]&#xff09;。机器人每次只能向下或者向右移动一步。…

HarmonyOS:给您的应用添加通知

一、通知介绍 通知旨在让用户以合适的方式及时获得有用的新消息&#xff0c;帮助用户高效地处理任务。应用可以通过通知接口发送通知消息&#xff0c;用户可以通过通知栏查看通知内容&#xff0c;也可以点击通知来打开应用&#xff0c;通知主要有以下使用场景&#xff1a; 显示…

Unity飞行代码 超仿真 保姆级教程

本文使用Rigidbody控制飞机&#xff0c;基本不会穿模。 效果 飞行效果 这是一条优雅的广告 如果你也在开发飞机大战等类型的飞行游戏&#xff0c;欢迎在主页搜索博文并参考。 搜索词&#xff1a;Unity游戏(Assault空对地打击)开发。 脚本编写 首先是完整代码。 using System.Co…