Socket
Socket
Sign inDemoInstall

balanced-match-all

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

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.


Version published
Weekly downloads
3
increased by200%
Maintainers
1
Install size
12.0 kB
Created
Weekly downloads
 

Readme

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

Last updated on 19 Jul 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc