Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

balanced-match-all

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

balanced-match-all

Recursively match balanced string pairs, like { and } or and using balanced-match and add level information to the matches.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

What?

Recursively match balanced string pairs, like { and } or and using balanced-match and add level information to the matches.

Example

yarn add balanced-match-all


    const match_all = require("balanced-match-all")

    let str = 'pre{in{nested{deep-nested}}}post'
    let arr = match_all('{', '}', str);

    console.log(arr);

    console.log(arr.map(o => o.body));

This will output:

    [
        {
            "start": 3,
            "end": 27,
            "pre": "pre",
            "body": "in{nested{deep-nested}}",
            "post": "post",
            "level": 0
        },
        {
            "start": 6,
            "end": 26,
            "pre": "in",
            "body": "nested{deep-nested}",
            "post": "",
            "level": 1
        },
        {
            "start": 13,
            "end": 25,
            "pre": "nested",
            "body": "deep-nested",
            "post": "",
            "level": 2
        }
    ]

API

The API is exactly the same as balanced-match.

var 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', '}'].

Keywords

FAQs

Package last updated on 19 Jul 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc