推荐答案
JavaScript 提供了多种编码和解码方法,用于处理字符串,尤其是 URL 和二进制数据。主要分为以下几类:
URI 编码/解码 (encodeURI, decodeURI, encodeURIComponent, decodeURIComponent)
encodeURI(uri): 用于编码整个 URI,不会编码以下字符:A-Z a-z 0-9 ; / ? : @ & = + $ , - _ . ! ~ * ' ( ) #。主要用于编码整个 URI。decodeURI(encodedURI): 用于解码由encodeURI()编码的 URI。encodeURIComponent(uriComponent): 用于编码 URI 的组件部分,会编码以下字符:; / ? : @ & = + $ , - _ . ! ~ * ' ( ) #以及encodeURI()中不会编码的字符。常用于编码 URL 的参数部分。decodeURIComponent(encodedURIComponent): 用于解码由encodeURIComponent()编码的 URI 组件。
区别:
encodeURI用于编码完整的URI,保留了URI的结构字符。encodeURIComponent用于编码URI的组件,会编码更多的字符,包括URI结构字符,适用于URL参数。
Base64 编码/解码 (btoa, atob)
btoa(string): 用于将字符串或二进制数据编码为 Base64 字符串。atob(encodedString): 用于解码 Base64 字符串,返回原始字符串或二进制数据。
注意:
btoa()和atob()主要用于处理 ASCII 字符编码,如果字符串包含非 ASCII 字符,需要先将其转换为 UTF-8 编码的字节序列,再进行 Base64 编码/解码,可以使用TextEncoder和TextDecoder实现。
本题详细解读
URI 编码和解码
URI 编码和解码是 Web 开发中常见的操作,主要用于处理 URL 中的特殊字符,确保 URL 的正确解析和传输。
encodeURI(uri)encodeURI()方法将 URI 中的某些字符替换为由一个、两个或三个十六进制数字编码的转义序列。它旨在编码完整的 URI,并且不会编码一些保留的 URI 结构字符,例如:A-Z a-z 0-9: 英文字母和数字; / ? : @ & = + $ , - _ . ! ~ * ' ( ) #: 一些 URI 结构字符
使用场景: 主要用于编码整个 URI,例如:
const uri = "https://example.com/path?query=hello world"; const encodedURI = encodeURI(uri); console.log(encodedURI); // Output: "https://example.com/path?query=hello%20world"
decodeURI(encodedURI)decodeURI()方法用于解码由encodeURI()编码的 URI,将转义序列恢复为原始字符。const encodedURI = "https://example.com/path?query=hello%20world"; const decodedURI = decodeURI(encodedURI); console.log(decodedURI); // Output: "https://example.com/path?query=hello world"
encodeURIComponent(uriComponent)encodeURIComponent()方法用于编码 URI 的组件部分,它会编码更多的字符,包括encodeURI()中不会编码的 URI 结构字符。A-Z a-z 0-9: 英文字母和数字; / ? : @ & = + $ , - _ . ! ~ * ' ( ) #: 一些 URI 结构字符
使用场景: 主要用于编码 URI 的组件,例如 URL 参数、路径等。
const uriComponent = "你好/世界?name=john doe&age=30"; const encodedComponent = encodeURIComponent(uriComponent); console.log(encodedComponent); // Output: "%E4%BD%A0%E5%A5%BD%2F%E4%B8%96%E7%95%8C%3Fname%3Djohn%20doe%26age%3D30"
decodeURIComponent(encodedURIComponent)decodeURIComponent()方法用于解码由encodeURIComponent()编码的 URI 组件。const encodedComponent = "%E4%BD%A0%E5%A5%BD%2F%E4%B8%96%E7%95%8C%3Fname%3Djohn%20doe%26age%3D30"; const decodedComponent = decodeURIComponent(encodedComponent); console.log(decodedComponent); // Output: "你好/世界?name=john doe&age=30"
Base64 编码和解码
Base64 是一种用 64 个字符表示任意二进制数据的编码方式。
btoa(string)btoa()方法用于将字符串或二进制数据编码为 Base64 字符串。注意:
btoa()只能处理 ASCII 字符编码,如果字符串中包含非 ASCII 字符,例如中文字符,需要先进行 UTF-8 编码,然后再进行 Base64 编码。可以使用TextEncoder将字符串转换为 UTF-8 字节序列的Uint8Array,然后再使用String.fromCharCode.apply(null, uint8Array)将Uint8Array转换为 ASCII 字符串。// www.javascriptcn.com code example function base64Encode(str) { try{ return btoa(str); } catch (e){ //处理非ascii字符 const encoder = new TextEncoder(); const uint8Array = encoder.encode(str); return btoa(String.fromCharCode.apply(null, uint8Array)); } } const str = "Hello, 世界!"; const encodedStr = base64Encode(str); console.log(encodedStr); // Output: SGVsbG8sIOS4lueVjCE=atob(encodedString)atob()方法用于解码 Base64 字符串,将其恢复为原始字符串。类似地,对于包含非 ASCII 字符的 Base64 编码,需要先解码为字节序列,再使用TextDecoder将字节序列解码为 UTF-8 字符串。// www.javascriptcn.com code example function base64Decode(encodedStr) { try{ return atob(encodedStr); }catch(e){ //处理非ascii字符 const uint8Array = Uint8Array.from(atob(encodedStr), char => char.charCodeAt(0)); const decoder = new TextDecoder(); return decoder.decode(uint8Array); } } const encodedStr = "SGVsbG8sIOS4lueVjCE="; const decodedStr = base64Decode(encodedStr); console.log(decodedStr); // Output: "Hello, 世界!"