
Security News
npm Introduces minimumReleaseAge and Bulk OIDC Configuration
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.
balanced-match
Advanced tools
Match balanced string pairs, like { and } or <b> and
</b>. Supports regular expressions as well!
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' }
For the first non-nested matching pair of a and b in str, return an
object with those keys:
aba and b not includeda and b not includeda and b not includedIf 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', '}'].
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].
The brace-expansion package is similar to balanced-match in that it deals with matching patterns within strings, specifically brace expansion patterns. It is more specialized for brace pattern expansion and does not provide the generic balanced string matching capabilities of balanced-match.
FAQs
Match balanced character pairs, like "{" and "}"
The npm package balanced-match receives a total of 111,356,824 weekly downloads. As such, balanced-match popularity was classified as popular.
We found that balanced-match demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.

Security News
AI agents are writing more code than ever, and that's creating new supply chain risks. Feross joins the Risky Business Podcast to break down what that means for open source security.

Research
/Security News
Socket uncovered four malicious NuGet packages targeting ASP.NET apps, using a typosquatted dropper and localhost proxy to steal Identity data and backdoor apps.