平衡匹配
匹配平衡的字符串对, 如 { 财务报告和审定财务报表 } 或者说 <b> 财务报告和审定财务报表
</b>。也支持正则表达式!
示例
获得第一对匹配的牙套 :
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'),
)
比赛是:
$ node example.js
{ 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 = 平衡(a、b、str)
首对不记名的配对 a 财务报告和审定财务报表 b 内 str,返回一个
带有这些密钥的对象 :
- 开始 第一个匹配的索引
a - 结束 匹配的索引
b - 预览 序言,
a财务报告和审定财务报表b未包括 - 机构 比赛,
a财务报告和审定财务报表b未包括 - 员额 上面写着:,
a财务报告和审定财务报表b未包括
如果没有匹配, undefined 将返回.
如果 str 包含更多 a 比起来 b / 有未匹配的对相, 第一种被关闭的对相将被使用. 譬如说, {{a} 将匹配 ['{', 'a', ''] 财务报告和审定财务报表 {a}} 将匹配 ['', 'a', '}']。 。 。 .
const r = 范围(a、b、str)
首对不记名的配对 a 财务报告和审定财务报表 b 内 str,返回一个
带有索引的矩阵 : [ <a index>, <b index> ]。 。 。 .
import { range } from 'balanced-match'
console.log(range('{', '}', 'pre{in{nested}}post'))
// [ 3, 14 ]
如果没有匹配, undefined 将返回.
如果 str 包含更多 a 比起来 b / 有未匹配的对相, 第一种被关闭的对相将被使用. 譬如说, {{a} 将匹配 [ 1, 3 ] 财务报告和审定财务报表 {a}} 将匹配 [0, 2]。 。 。 .