3 Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
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)
8 [![testling badge](https://ci.testling.com/juliangruber/balanced-match.png)](https://ci.testling.com/juliangruber/balanced-match)
12 Get the first matching pair of braces:
15 var balanced = require('balanced-match');
17 console.log(balanced('{', '}', 'pre{in{nested}}post'));
18 console.log(balanced('{', '}', 'pre{first}between{second}post'));
19 console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'));
26 { start: 3, end: 14, pre: 'pre', body: 'in{nested}', post: 'post' }
31 post: 'between{second}post' }
32 { start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }
37 ### var m = balanced(a, b, str)
39 For the first non-nested matching pair of `a` and `b` in `str`, return an
40 object with those keys:
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
48 If there's no match, `undefined` will be returned.
50 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', '}']`.
52 ### var r = balanced.range(a, b, str)
54 For the first non-nested matching pair of `a` and `b` in `str`, return an
55 array with indexes: `[ <a index>, <b index> ]`.
57 If there's no match, `undefined` will be returned.
59 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]`.
63 With [npm](https://npmjs.org) do:
66 npm install balanced-match
69 ## Security contact information
71 To report a security vulnerability, please use the
72 [Tidelift security contact](https://tidelift.com/security).
73 Tidelift will coordinate the fix and disclosure.
79 Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
81 Permission is hereby granted, free of charge, to any person obtaining a copy of
82 this software and associated documentation files (the "Software"), to deal in
83 the Software without restriction, including without limitation the rights to
84 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
85 of the Software, and to permit persons to whom the Software is furnished to do
86 so, subject to the following conditions:
88 The above copyright notice and this permission notice shall be included in all
89 copies or substantial portions of the Software.
91 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
92 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
93 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
94 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
95 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
96 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE