balanced-match

中文文档
5.0.0发布于 2026/10/17MITTypeScriptESM + CJS零依赖近 30 天下载 19亿

Match balanced character pairs, like "{" and "}"

原始使用文档
文档目录4 节
  1. Example
  2. API
  3. const m = balanced(a, b, str)
  4. const r = range(a, b, str)

balanced-match

Match balanced string pairs, like { and } or <b> and </b>. Supports regular expressions as well!

Example

Get the first matching pair of braces:

import { balanced } from 'balanced-match'

console.log(balanced('{', '}', 'pre{in{nested}}post'))
console.log(balanced('{', '}', 'pre{first}between{second}post'))
console.log(
  balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre  {   in{nest}   }  post'),
)

The matches are:

$ node example.js
{ start: 3, end: 14, pre: 'pre', body: 'in{nested}', post: 'post' }
{ start: 3,
  end: 9,
  pre: 'pre',
  body: 'first',
  post: 'between{second}post' }
{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }

API

const m = balanced(a, b, str)

For the first non-nested matching pair of a and b in str, return an object with those keys:

  • start the index of the first match of a
  • end the index of the matching b
  • pre the preamble, a and b not included
  • body the match, a and b not included
  • post the postscript, a and b not included

If there's no match, undefined will be returned.

If the str contains more a than b / there are unmatched pairs, the first match that was closed will be used. For example, {{a} will match ['{', 'a', ''] and {a}} will match ['', 'a', '}'].

const r = range(a, b, str)

For the first non-nested matching pair of a and b in str, return an array with indexes: [ <a index>, <b index> ].

import { range } from 'balanced-match'

console.log(range('{', '}', 'pre{in{nested}}post'))
// [ 3, 14 ]

If there's no match, undefined will be returned.

If the str contains more a than b / there are unmatched pairs, the first match that was closed will be used. For example, {{a} will match [ 1, 3 ] and {a}} will match [0, 2].

版本与发布记录

balanced-match 共发布 19 个版本,首次发布于 2013/10/13;过去 12 个月发布了 4 个版本。

版本发布日期距今
5.0.0latest2026/10/1722 天前
4.0.42026/02/228 个月前
4.0.32026/02/188 个月前
4.0.22026/02/098 个月前
3.0.12023/10/073 年前
3.0.02023/10/053 年前

关于 balanced-match 的常见问题

balanced-match 是什么?

Match balanced character pairs, like "{" and "}"

如何安装 balanced-match?

在项目目录运行 npm install balanced-match,或使用 pnpm add / yarn add / bun add。

balanced-match 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

balanced-match 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

balanced-match 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 20 || >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

balanced-match 可以商用吗?

balanced-match 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

balanced-match 的下载量有多少?

根据 npm 官方下载统计,balanced-match 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,898,306,380 次,日均约 63,276,879 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

balanced-match 还在维护吗?

最新版本 5.0.0 发布于 2026/10/17(22 天前);至今共发布 19 个版本,其中过去 12 个月发布了 4 个;首次发布于 2013/10/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。