UNPKG

3.4 kBMarkdownView Raw
1# balanced-match
2
3Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
4
5[![CI](https://github.com/juliangruber/balanced-match/actions/workflows/ci.yml/badge.svg)](https://github.com/juliangruber/balanced-match/actions/workflows/ci.yml)
6[![downloads](https://img.shields.io/npm/dm/balanced-match.svg)](https://www.npmjs.org/package/balanced-match)
7
8## Example
9
10Get the first matching pair of braces:
11
12```js
13import balanced from 'balanced-match'
14
15console.log(balanced('{', '}', 'pre{in{nested}}post'))
16console.log(balanced('{', '}', 'pre{first}between{second}post'))
17console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'))
18```
19
20The matches are:
21
22```bash
23$ node example.js
24{ start: 3, end: 14, pre: 'pre', body: 'in{nested}', post: 'post' }
25{ start: 3,
26 end: 9,
27 pre: 'pre',
28 body: 'first',
29 post: 'between{second}post' }
30{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }
31```
32
33## API
34
35### const m = balanced(a, b, str)
36
37For the first non-nested matching pair of `a` and `b` in `str`, return an
38object with those keys:
39
40- **start** the index of the first match of `a`
41- **end** the index of the matching `b`
42- **pre** the preamble, `a` and `b` not included
43- **body** the match, `a` and `b` not included
44- **post** the postscript, `a` and `b` not included
45
46If there's no match, `undefined` will be returned.
47
48If 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', '}']`.
49
50### const r = balanced.range(a, b, str)
51
52For the first non-nested matching pair of `a` and `b` in `str`, return an
53array with indexes: `[ <a index>, <b index> ]`.
54
55If there's no match, `undefined` will be returned.
56
57If 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]`.
58
59## Installation
60
61With [npm](https://npmjs.org) do:
62
63```bash
64npm install balanced-match
65```
66
67## Security contact information
68
69To report a security vulnerability, please use the
70[Tidelift security contact](https://tidelift.com/security).
71Tidelift will coordinate the fix and disclosure.
72
73## License
74
75(MIT)
76
77Copyright (c) 2013 Julian Gruber &lt;julian@juliangruber.com&gt;
78
79Permission is hereby granted, free of charge, to any person obtaining a copy of
80this software and associated documentation files (the "Software"), to deal in
81the Software without restriction, including without limitation the rights to
82use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
83of the Software, and to permit persons to whom the Software is furnished to do
84so, subject to the following conditions:
85
86The above copyright notice and this permission notice shall be included in all
87copies or substantial portions of the Software.
88
89THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
95SOFTWARE.