1 | # balanced-match
|
2 |
|
3 | Match 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 |
|
10 | Get the first matching pair of braces:
|
11 |
|
12 | ```js
|
13 | import balanced from 'balanced-match'
|
14 |
|
15 | console.log(balanced('{', '}', 'pre{in{nested}}post'))
|
16 | console.log(balanced('{', '}', 'pre{first}between{second}post'))
|
17 | console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'))
|
18 | ```
|
19 |
|
20 | The 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 |
|
37 | For the first non-nested matching pair of `a` and `b` in `str`, return an
|
38 | object 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 |
|
46 | If there's no match, `undefined` will be returned.
|
47 |
|
48 | 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', '}']`.
|
49 |
|
50 | ### const r = balanced.range(a, b, str)
|
51 |
|
52 | For the first non-nested matching pair of `a` and `b` in `str`, return an
|
53 | array with indexes: `[ <a index>, <b index> ]`.
|
54 |
|
55 | If there's no match, `undefined` will be returned.
|
56 |
|
57 | 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]`.
|
58 |
|
59 | ## Installation
|
60 |
|
61 | With [npm](https://npmjs.org) do:
|
62 |
|
63 | ```bash
|
64 | npm install balanced-match
|
65 | ```
|
66 |
|
67 | ## Security contact information
|
68 |
|
69 | To report a security vulnerability, please use the
|
70 | [Tidelift security contact](https://tidelift.com/security).
|
71 | Tidelift will coordinate the fix and disclosure.
|
72 |
|
73 | ## License
|
74 |
|
75 | (MIT)
|
76 |
|
77 | Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
|
78 |
|
79 | Permission is hereby granted, free of charge, to any person obtaining a copy of
|
80 | this software and associated documentation files (the "Software"), to deal in
|
81 | the Software without restriction, including without limitation the rights to
|
82 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
83 | of the Software, and to permit persons to whom the Software is furnished to do
|
84 | so, subject to the following conditions:
|
85 |
|
86 | The above copyright notice and this permission notice shall be included in all
|
87 | copies or substantial portions of the Software.
|
88 |
|
89 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
90 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
91 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
92 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
93 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
94 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
95 | SOFTWARE.
|