You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.2 KiB

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