# balanced-match

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

Latest version **4.0.4** (published 2026-02-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install balanced-match
pnpm add balanced-match
yarn add balanced-match
bun add balanced-match
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.4 |
| Published | 2026-02-22 |
| First published | 2013-10-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | 18 \|\| 20 \|\| >=22 |
| Dependencies | 0 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 132 |
| Maintainers | juliangruber |
| Keywords | match, regexp, test, balanced, parse |

## Links

- npm: https://www.npmjs.com/package/balanced-match
- Repository: https://github.com/juliangruber/balanced-match
- Homepage: https://github.com/juliangruber/balanced-match#readme
- Issues: https://github.com/juliangruber/balanced-match/issues
- npm.io page: https://npm.io/package/balanced-match

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 4.0.4 (latest) — 2026-02-22
- 4.0.3 — 2026-02-18
- 4.0.2 — 2026-02-09
- 3.0.1 — 2023-10-07
- 3.0.0 — 2023-10-05
- 2.0.0 — 2021-04-06
- 1.0.2 — 2021-04-06
- 1.0.1 — 2021-04-06
- 1.0.0 — 2017-06-12
- 0.4.2 — 2016-07-18
- 0.4.1 — 2016-05-01
- 0.4.0 — 2016-04-07
- 0.3.0 — 2015-11-28
- 0.2.1 — 2015-10-22
- 0.2.0 — 2014-11-30
- … 3 more at https://npm.io/package/balanced-match/versions

## README

# 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:

```js
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:

```bash
$ 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 = balanced.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> ]`.

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]`.

---
_Source: https://npm.io/package/balanced-match · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
