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.

README.md 3.4 KiB

10 maanden geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. ## Security contact information
  47. To report a security vulnerability, please use the
  48. [Tidelift security contact](https://tidelift.com/security).
  49. Tidelift will coordinate the fix and disclosure.
  50. ## License
  51. (MIT)
  52. Copyright (c) 2013 Julian Gruber &lt;julian@juliangruber.com&gt;
  53. Permission is hereby granted, free of charge, to any person obtaining a copy of
  54. this software and associated documentation files (the "Software"), to deal in
  55. the Software without restriction, including without limitation the rights to
  56. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  57. of the Software, and to permit persons to whom the Software is furnished to do
  58. so, subject to the following conditions:
  59. The above copyright notice and this permission notice shall be included in all
  60. copies or substantial portions of the Software.
  61. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  62. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  63. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  64. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  65. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  66. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  67. SOFTWARE.