balanced-match
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Balanced-match is a JavaScript library designed for matching balanced string pairs, such as "{" and "}" or "<b>" and "</b>". It also provides flexible support for regular expressions. Qualifying this further, the library doesn't just identify the occurrences of these pairs, but discerns their balanced relationships within a given string, taking into account the opening and closing sequence, nested pairs, and more.
The use of the balanced-match library is straightforward and intuitive. Firstly, you need to install it via npm, by running npm install balanced-match
in your terminal. Once it's installed, you can require it in your JavaScript file as follows:
var balanced = require('balanced-match');
Thereafter, you can utilize the library to match balanced string pairs. Here's an example on how you could extract the first matching pair of braces:
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 output of these console.log statements display the details of the matched pairs such as their start and end indices within the string, and the preamble and postscript text excluding the pair elements.
The complete documentation for balanced-match is available on its GitHub repository, accessible via the URL git://github.com/juliangruber/balanced-match.git
. It provides detailed information about the API's parameters, the installation process, use cases, and more. The documentation extends to describing the output format and offering valuable insights on what to expect when dealing with unbalanced (misleading) pairs.