- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
Node.js v24.13.0 文档
- Node.js v24.13.0
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- module/typescript TS 模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- sea 单个可执行应用程序
- 其他版本
查询字符串#>
【Query string】
源代码: lib/querystring.js
node:querystring 模块提供了解析和格式化 URL 查询字符串的实用工具。可以使用以下方式访问:
【The node:querystring module provides utilities for parsing and formatting URL
query strings. It can be accessed using:】
const querystring = require('node:querystring');
querystring 的性能比 <URLSearchParams> 更高,但它不是标准化的 API。当性能不是关键或希望与浏览器代码兼容时,请使用 <URLSearchParams>。
querystring.decode()#>
querystring.decode() 函数是 querystring.parse() 的别名。
【The querystring.decode() function is an alias for querystring.parse().】
querystring.encode()#>
querystring.encode() 函数是 querystring.stringify() 的别名。
【The querystring.encode() function is an alias for querystring.stringify().】
querystring.escape(str)#>
str<string>
querystring.escape() 方法对给定的 str 执行 URL 百分比编码,其方式针对 URL 查询字符串的特定要求进行了优化。
【The querystring.escape() method performs URL percent-encoding on the given
str in a manner that is optimized for the specific requirements of URL
query strings.】
querystring.escape() 方法被 querystring.stringify() 使用,通常不期望直接使用它。它之所以被导出,主要是为了允许应用代码在必要时通过将 querystring.escape 指定为其他函数来提供替代的百分比编码实现。
【The querystring.escape() method is used by querystring.stringify() and is
generally not expected to be used directly. It is exported primarily to allow
application code to provide a replacement percent-encoding implementation if
necessary by assigning querystring.escape to an alternative function.】
querystring.parse(str[, sep[, eq[, options]]])#>
str<string> 要解析的 URL 查询字符串sep<string> 用于分隔查询字符串中键值对的子字符串。默认值:'&'。eq<string>。用于在查询字符串中分隔键和值的子字符串。默认值:'='。options<Object>decodeURIComponent<Function> 用于解码查询字符串中百分比编码字符的函数。默认值:querystring.unescape()。maxKeys<number> 指定要解析的最大键数。指定0可取消键计数限制。默认值:1000。
querystring.parse() 方法将 URL 查询字符串(str)解析成一组键值对。
【The querystring.parse() method parses a URL query string (str) into a
collection of key and value pairs.】
例如,查询字符串 'foo=bar&abc=xyz&abc=123' 被解析为:
【For example, the query string 'foo=bar&abc=xyz&abc=123' is parsed into:】
{
"foo": "bar",
"abc": ["xyz", "123"]
}
querystring.parse() 方法返回的对象不会从 JavaScript 的 Object 原型继承。这意味着像 obj.toString()、obj.hasOwnProperty() 等典型的 Object 方法未定义,并且无法使用。
【The object returned by the querystring.parse() method does not
prototypically inherit from the JavaScript Object. This means that typical
Object methods such as obj.toString(), obj.hasOwnProperty(), and others
are not defined and will not work.】
默认情况下,查询字符串中的百分号编码字符将被假定使用 UTF-8 编码。如果使用了其他字符编码,则需要指定一个替代的 decodeURIComponent 选项:
【By default, percent-encoded characters within the query string will be assumed
to use UTF-8 encoding. If an alternative character encoding is used, then an
alternative decodeURIComponent option will need to be specified:】
// Assuming gbkDecodeURIComponent function already exists...
querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,
{ decodeURIComponent: gbkDecodeURIComponent });
querystring.stringify(obj[, sep[, eq[, options]]])#>
obj<Object> 要序列化为 URL 查询字符串的对象sep<string> 用于分隔查询字符串中键值对的子字符串。默认值:'&'。eq<string>。用于在查询字符串中分隔键和值的子字符串。默认值:'='。optionsencodeURIComponent<Function> 在将 URL 不安全字符转换为查询字符串中的百分号编码时使用的函数。默认值:querystring.escape()。
querystring.stringify() 方法通过遍历对象的“自有属性”,从给定的 obj 生成 URL 查询字符串。
【The querystring.stringify() method produces a URL query string from a
given obj by iterating through the object's "own properties".】
它会序列化传入 obj 的以下类型的值:
<string> | <number> | <bigint> | <boolean> | <string[]> | <number[]> | <bigint[]> | <boolean[]>
数值必须是有限的。任何其他输入值都会被强制转换为空字符串。
【It serializes the following types of values passed in obj:
<string> | <number> | <bigint> | <boolean> | <string[]> | <number[]> | <bigint[]> | <boolean[]>
The numeric values must be finite. Any other input values will be coerced to
empty strings.】
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
// Returns 'foo=bar&baz=qux&baz=quux&corge='
querystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
// Returns 'foo:bar;baz:qux'
默认情况下,查询字符串中需要进行百分比编码的字符将会以 UTF-8 编码。如果需要使用其他编码,则需要指定一个替代的 encodeURIComponent 选项:
【By default, characters requiring percent-encoding within the query string will
be encoded as UTF-8. If an alternative encoding is required, then an alternative
encodeURIComponent option will need to be specified:】
// Assuming gbkEncodeURIComponent function already exists,
querystring.stringify({ w: '中文', foo: 'bar' }, null, null,
{ encodeURIComponent: gbkEncodeURIComponent });
querystring.unescape(str)#>
str<string>
querystring.unescape() 方法对给定的 str 中的 URL 百分号编码字符进行解码。
【The querystring.unescape() method performs decoding of URL percent-encoded
characters on the given str.】
querystring.unescape() 方法由 querystring.parse() 使用,通常不建议直接使用它。它之所以被导出,主要是为了在必要时允许应用代码通过将 querystring.unescape 指定为其他函数来提供替代的解码实现。
【The querystring.unescape() method is used by querystring.parse() and is
generally not expected to be used directly. It is exported primarily to allow
application code to provide a replacement decoding implementation if
necessary by assigning querystring.unescape to an alternative function.】
默认情况下,querystring.unescape() 方法会尝试使用 JavaScript 内置的 decodeURIComponent() 方法进行解码。如果解码失败,将使用一个更安全的等效方法,该方法在遇到格式错误的 URL 时不会抛出异常。
【By default, the querystring.unescape() method will attempt to use the
JavaScript built-in decodeURIComponent() method to decode. If that fails,
a safer equivalent that does not throw on malformed URLs will be used.】