我认为您可能遇到了 minimatch(或 fnmatch(3) 的任何实现)和 globstar 的限制。可能值得注意的是,我所知道的 fnmatch 的 C 实现实际上并没有实现 globstar,但由于 fnmatch impls(包括 minimatch)服务于他们的 globers 的利益,这可能会有所不同。
您认为应该工作的 glob 在用作 glob 时确实有效。
$ find . -type f
./docs/foo/thing.js
./docs/thing.js
./docs/nope.txt
./docs-src/foo/thing.js
./docs-src/thing.js
./x.sh
./client/docs/foo/thing.js
./client/docs/thing.js
./client/docs/nope.txt
./client/docs-src/foo/thing.js
./client/docs-src/thing.js
./client/docs-src/nope.txt
./client/nope.txt
./src/foo/thing.js
./src/thing.js
$ for i in ./!(docs)/**/*.js; do echo $i; done
./client/docs-src/foo/thing.js
./client/docs-src/thing.js
./client/docs/foo/thing.js
./client/docs/thing.js
./docs-src/foo/thing.js
./docs-src/thing.js
./src/foo/thing.js
./src/thing.js
$ node -p 'require("glob").sync("./!(docs)/**/*.js")'
[ './client/docs-src/foo/thing.js',
'./client/docs-src/thing.js',
'./client/docs/foo/thing.js',
'./client/docs/thing.js',
'./docs-src/foo/thing.js',
'./docs-src/thing.js',
'./src/foo/thing.js',
'./src/thing.js' ]
编辑:哦,我明白了,你只想匹配任何文件夹深度的东西,这些东西在路径的任何地方都没有任何路径 docs
部分。不,这不可能以支持任意深度的方式实现,例如 glob 或 minimatch 模式。您必须使用排除项,或构造一个如下所示的 glob:{!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js
否则,类似的路径x/docs/y/z.js
将**/!(docs)/**/*.js
通过说第一个**
不匹配任何内容、!(docs)
匹配x
、下一个**
匹配docs/y
然后*.js
匹配来匹配z.js
。