# balanced-match **Repository Path**: ArkTSCentralRepository/balanced-match ## Basic Information - **Project Name**: balanced-match - **Description**: balanced-match 是在 ArkTS 中实现的库,用于匹配平衡的字符串对,如 { 和 } 或 ,支持正则表达式,并返回匹配的起始和结束位置,以及匹配的内容。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-21 - **Last Updated**: 2024-11-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # balanced-match 基于[balanced-match](https://www.npmjs.com/package/balanced-match)原库3.0.1版本进行适配, 所有功能代码已经转换为`ArkTS`文件 ## Install ```sh ohpm install balanced-match ``` ## Description Match balanced string pairs, like `{` and `}` or `` and ``. Supports regular expressions as well! ## Example Get the first matching pair of braces: ```typescript import balanced from 'balanced-match' console.log(balanced('{', '}', 'pre{in{nested}}post')) console.log(balanced('{', '}', 'pre{first}between{second}post')) console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post')) ``` The matches are: ```bash { start: 3, end: 14, pre: 'pre', body: 'in{nested}', post: 'post' } { start: 3, end: 9, pre: 'pre', body: 'first', post: 'between{second}post' } { start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' } ``` ## API ### const m = balanced(a, b, str) For the first non-nested matching pair of `a` and `b` in `str`, return an object with those keys: - **start** the index of the first match of `a` - **end** the index of the matching `b` - **pre** the preamble, `a` and `b` not included - **body** the match, `a` and `b` not included - **post** the postscript, `a` and `b` not included If there's no match, `undefined` will be returned. 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', '}']`. ### const r = balanced.range(a, b, str) For the first non-nested matching pair of `a` and `b` in `str`, return an array with indexes: `[ , ]`. If there's no match, `undefined` will be returned. 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]`. ## Security contact information To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. ## License MIT