UNPKG

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