Match balanced string pairs, like {\ttfamily \{} and {\ttfamily \}} or {\ttfamily $<$b$>$} and {\ttfamily $<$/b$>$}. Supports regular expressions as well!

\href{http://travis-ci.org/juliangruber/balanced-match}{\texttt{ }} \href{https://www.npmjs.org/package/balanced-match}{\texttt{ }}

\href{https://ci.testling.com/juliangruber/balanced-match}{\texttt{ }}\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3799}{}\doxysection{Example}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3799}
Get the first matching pair of braces\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var balanced = require('balanced-\/match');}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(balanced('\{', '\}', 'pre\{in\{nested\}\}post'));}
\DoxyCodeLine{console.log(balanced('\{', '\}', 'pre\{first\}between\{second\}post'));}
\DoxyCodeLine{console.log(balanced(/\(\backslash\)s+\(\backslash\)\{\(\backslash\)s+/, /\(\backslash\)s+\(\backslash\)\}\(\backslash\)s+/, 'pre  \{   in\{nest\}   \}  post')); }

\end{DoxyCode}


The matches are\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ node example.js}
\DoxyCodeLine{\{ start: 3, end: 14, pre: 'pre', body: 'in\{nested\}', post: 'post' \}}
\DoxyCodeLine{\{ start: 3,}
\DoxyCodeLine{  end: 9,}
\DoxyCodeLine{  pre: 'pre',}
\DoxyCodeLine{  body: 'first',}
\DoxyCodeLine{  post: 'between\{second\}post' \}}
\DoxyCodeLine{\{ start: 3, end: 17, pre: 'pre', body: 'in\{nest\}', post: 'post' \} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3800}{}\doxysection{API}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3800}
\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3801}{}\doxysubsection{var m = balanced(a, b, str)}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3801}
For the first non-\/nested matching pair of {\ttfamily a} and {\ttfamily b} in {\ttfamily str}, return an object with those keys\+:


\begin{DoxyItemize}
\item {\bfseries{start}} the index of the first match of {\ttfamily a}
\item {\bfseries{end}} the index of the matching {\ttfamily b}
\item {\bfseries{pre}} the preamble, {\ttfamily a} and {\ttfamily b} not included
\item {\bfseries{body}} the match, {\ttfamily a} and {\ttfamily b} not included
\item {\bfseries{post}} the postscript, {\ttfamily a} and {\ttfamily b} not included
\end{DoxyItemize}

If there\textquotesingle{}s no match, {\ttfamily undefined} will be returned.

If the {\ttfamily str} contains more {\ttfamily a} than {\ttfamily b} / there are unmatched pairs, the first match that was closed will be used. For example, {\ttfamily \{\{a\}} will match `\mbox{[}'\{\textquotesingle{}, \textquotesingle{}a\textquotesingle{}, \textquotesingle{}\textquotesingle{}\mbox{]}{\ttfamily and}\{a\}\}{\ttfamily will match}\mbox{[}\textquotesingle{}\textquotesingle{}, \textquotesingle{}a\textquotesingle{}, \textquotesingle{}\}\textquotesingle{}\mbox{]}\`{}.\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3802}{}\doxysubsection{var r = balanced.\+range(a, b, str)}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3802}
For the first non-\/nested matching pair of {\ttfamily a} and {\ttfamily b} in {\ttfamily str}, return an array with indexes\+: {\ttfamily \mbox{[} $<$a index$>$, $<$b index$>$ \mbox{]}}.

If there\textquotesingle{}s no match, {\ttfamily undefined} will be returned.

If the {\ttfamily str} contains more {\ttfamily a} than {\ttfamily b} / there are unmatched pairs, the first match that was closed will be used. For example, {\ttfamily \{\{a\}} will match {\ttfamily \mbox{[} 1, 3 \mbox{]}} and {\ttfamily \{a\}\}} will match {\ttfamily \mbox{[}0, 2\mbox{]}}.\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3803}{}\doxysection{Installation}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3803}
With \href{https://npmjs.org}{\texttt{ npm}} do\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{npm install balanced-\/match }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_balanced_match_README_autotoc_md3804}{}\doxysection{License}\label{md_sfcode_node_modules_balanced_match_README_autotoc_md3804}
(MIT)

Copyright (c) 2013 Julian Gruber $<$\href{mailto:julian@juliangruber.com}{\texttt{ julian@juliangruber.\+com}}$>$

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \char`\"{}\+Software\char`\"{}), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions\+:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED \char`\"{}\+AS IS\char`\"{}, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 