类 Bash 的花括号展开,使用 JavaScript 实现。相比其他花括号展开库更安全,完整支持 Bash 4.3 花括号规范,且不牺牲速度。
请考虑关注本项目作者 Jon Schlinkert,并考虑为项目加星以表达你的 ❤️ 和支持。
使用 npm 安装:
$ npm install --save braces
详情请参阅更新日志。
花括号模式通过增加匹配特定范围和字符序列的能力,使 glob 更加强大。
a/{b,c}/d => ['a/b/d', 'a/c/d']{01..03} => ['01', '02', '03']{2..10..2} => ['2', '4', '6', '8', '10']主要导出的是一个函数,它接收一个或多个花括号 patterns 和 options。
const braces = require('braces');
// braces(patterns[, options]);
console.log(braces(['{01..05}', '{a..e}']));
//=> ['(0[1-5])', '([a-e])']
console.log(braces(['{01..05}', '{a..e}'], { expand: true }));
//=> ['01', '02', '03', '04', '05', 'a', 'b', 'c', 'd', 'e']
默认情况下,花括号模式会被编译成经过优化的字符串,用于创建正则表达式和进行匹配。
已编译
console.log(braces('a/{x,y,z}/b'));
//=> ['a/(x|y|z)/b']
console.log(braces(['a/{01..20}/b', 'a/{1..5}/b']));
//=> [ 'a/(0[1-9]|1[0-9]|20)/b', 'a/([1-5])/b' ]
展开
通过将 expand 选项设置为 true,或使用 braces.expand()(返回一个数组,类似于你从 Bash、echo {1..5} 或 minimatch 中预期得到的结果)来启用花括号展开:
console.log(braces('a/{x,y,z}/b', { expand: true }));
//=> ['a/x/b', 'a/y/b', 'a/z/b']
console.log(braces.expand('{01..10}'));
//=> ['01','02','03','04','05','06','07','08','09','10']
展开列表(类似 Bash 的“集合”):
console.log(braces('a/{foo,bar,baz}/*.js'));
//=> ['a/(foo|bar|baz)/*.js']
console.log(braces.expand('a/{foo,bar,baz}/*.js'));
//=> ['a/foo/*.js', 'a/bar/*.js', 'a/baz/*.js']
展开字符范围(类似 Bash 的“序列”):
console.log(braces.expand('{1..3}')); // ['1', '2', '3']
console.log(braces.expand('a/{1..3}/b')); // ['a/1/b', 'a/2/b', 'a/3/b']
console.log(braces('{a..c}', { expand: true })); // ['a', 'b', 'c']
console.log(braces('foo/{a..c}', { expand: true })); // ['foo/a', 'foo/b', 'foo/c']
// supports zero-padded ranges
console.log(braces('a/{01..03}/b')); //=> ['a/(0[1-3])/b']
console.log(braces('a/{001..300}/b')); //=> ['a/(0{2}[1-9]|0[1-9][0-9]|[12][0-9]{2}|300)/b']
有关所有可用的范围扩展选项,请参阅 fill-range。
步长或增量可与范围一起使用:
console.log(braces.expand('{2..10..2}'));
//=> ['2', '4', '6', '8', '10']
console.log(braces('{2..10..2}'));
//=> ['(2|4|6|8|10)']
当使用 .optimize 方法,或将 options.optimize 设置为 true 时,序列会被传递给 to-regex-range 进行展开。
花括号模式可以嵌套。每个展开字符串的结果不会排序,并且保留从左到右的顺序。
“展开”花括号
console.log(braces.expand('a{b,c,/{x,y}}/e'));
//=> ['ab/e', 'ac/e', 'a/x/e', 'a/y/e']
console.log(braces.expand('a/{x,{1..5},y}/c'));
//=> ['a/x/c', 'a/1/c', 'a/2/c', 'a/3/c', 'a/4/c', 'a/5/c', 'a/y/c']
“优化”花括号
console.log(braces('a{b,c,/{x,y}}/e'));
//=> ['a(b|c|/(x|y))/e']
console.log(braces('a/{x,{1..5},y}/c'));
//=> ['a/(x|([1-5])|y)/c']
转义花括号
如果_左花括号或右花括号被转义_,则花括号模式不会被展开或求值:
console.log(braces.expand('a\\{d,c,b}e'));
//=> ['a{d,c,b}e']
console.log(braces.expand('a{d,c,b\\}e'));
//=> ['a{d,c,b}e']
转义逗号
花括号内的逗号也可以被转义:
console.log(braces.expand('a{b\\,c}d'));
//=> ['a{b,c}d']
console.log(braces.expand('a{d\\,c,b}e'));
//=> ['ad,ce', 'abe']
单个项
按照 bash 的惯例,当花括号模式只包含单个字符时,它也不会被展开:
console.log(braces.expand('a{b}c'));
//=> ['a{b}c']
类型:Number
默认值:10,000
描述:限制输入字符串的长度。当输入字符串是生成的,或你的应用允许用户传入字符串等场景下很有用。
console.log(braces('a/{b,c}/d', { maxLength: 3 })); //=> throws an error
类型:Boolean
默认值:undefined
描述:生成一个“展开”的花括号模式(或者你也可以使用 braces.expand() 方法,其作用相同)。
console.log(braces('a/{b,c}/d', { expand: true }));
//=> [ 'a/b/d', 'a/c/d' ]
类型:Boolean
默认值:undefined
描述:从返回的数组中移除重复项。
类型:Number
默认值:1000
描述:为防止用户传入恶意模式,当使用 braces.expand() 或 options.expand 为 true 且生成的 range 将超过 rangeLimit 时,会抛出错误。
你可以自定义 options.rangeLimit 或将其设置为 Inifinity 以完全禁用此限制。
示例
// pattern exceeds the "rangeLimit", so it's optimized automatically
console.log(braces.expand('{1..1000}'));
//=> ['([1-9]|[1-9][0-9]{1,2}|1000)']
// pattern does not exceed "rangeLimit", so it's NOT optimized
console.log(braces.expand('{1..100}'));
//=> ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100']
类型:Function
默认值:undefined
描述:自定义范围扩展。
示例:转换非数值
const alpha = braces.expand('x/{a..e}/y', {
transform(value, index) {
// When non-numeric values are passed, "value" is a character code.
return 'foo/' + String.fromCharCode(value) + '-' + index;
},
});
console.log(alpha);
//=> [ 'x/foo/a-0/y', 'x/foo/b-1/y', 'x/foo/c-2/y', 'x/foo/d-3/y', 'x/foo/e-4/y' ]
示例:转换数值
const numeric = braces.expand('{1..5}', {
transform(value) {
// when numeric values are passed, "value" is a number
return 'foo/' + value * 2;
},
});
console.log(numeric);
//=> [ 'foo/2', 'foo/4', 'foo/6', 'foo/8', 'foo/10' ]
类型:Boolean
默认值:undefined
描述:在正则表达式中,量词可用于指定某个标记可以重复的次数。例如,a{1,3} 将匹配字母 a 一到三次。
不幸的是,正则量词恰好与 Bash 列表 共享相同的语法。
quantifiers 选项告诉 braces 在给定模式中检测何时定义了 正则量词,并且不要尝试将它们作为列表展开。
示例
const braces = require('braces');
console.log(braces('a/b{1,3}/{x,y,z}'));
//=> [ 'a/b(1|3)/(x|y|z)' ]
console.log(braces('a/b{1,3}/{x,y,z}', { quantifiers: true }));
//=> [ 'a/b{1,3}/(x|y|z)' ]
console.log(braces('a/b{1,3}/{x,y,z}', { quantifiers: true, expand: true }));
//=> [ 'a/b{1,3}/x', 'a/b{1,3}/y', 'a/b{1,3}/z' ]
类型:Boolean
默认值:undefined
描述:不要从结果中去除用于转义的反斜杠。
花括号展开是一种参数展开,由 Unix shell 推广开来,用于生成字符串列表,以及与通配符(globs)配合使用时进行类似正则的匹配。
除了“展开”,花括号也用于匹配。换句话说: