4

我有一个看起来像这样的项目

ls foo/
- file0.js
- a/file1.js
- b/file2.js
- c/file3.js
- d/file4.js

如何编写glob 模式排除 c & d 文件夹但获取所有其他 javascript 文件?我在这里查看了一个示例,但无法正常工作。

我想解决方案看起来类似于:

glob('+(**/*.js|!(c|d))', function(err, file) {
  return console.log(f);
});

我想要回来

- file0.js
- a/file1.js
- b/file2.js
4

2 回答 2

6

对于没有第二个参数来设置排除的环境,我们可以使用下面示例中演示的模式来实现这样的异常:

图案

/src/**/!(els)/*.scss

结构体

/src/style/kit.scss
/src/style/els/some.scss
/src/style/els/two.scss

结果

它只会选择/src/style/kit.scss

我们可以使用http://www.globtester.comhttps://www.digitalocean.com/community/tools/glob在线快速测试。

在此处输入图像描述

更新

如果我们正在使用Gulp 任务运行器。或提供第二个或多个排除参数的其他一些工具或类。或者只是多个支持排除的全局选择器。然后我们可以像下面的例子一样为 gulp show 做:

对于 Gulp

我们传递一个数组而不仅仅是一个字符串(多个 glob 选择器,一个接一个地应用以添加更多文件或排除)

src(['src/style/**/*.{scss,sass}', '!(src/style/els/**)'])

我们可以有多个排除项

watch(['src/style/**/*.{scss,sass}', '!(src/style/els/**)', '!(src/style/_somefileToExclude.scss)'])

在 gulp 中,您可以使用任何支持数组作为全局选择器的方法。src并且watch是我用来做的。

注意:如果您想排除一个文件夹及其所有子文件夹,我们**如上所述使用而**/*不是不起作用。如果您需要一些特定的文件类型(扩展名),那么您可以使用**/*.scss例如。

于 2018-12-23T10:25:42.237 回答
3

There is an ignore option I glazed over in the readme:

glob('**/*.js', { ignore: '{c,d}/**' }, cb)

This will exclude both c + d folders from the match. More here

于 2015-03-12T22:17:51.623 回答