![](/img/trans.png)
[英]How to write a glob in javascript to exclude and include the files?
[英]How to write a single minimatch glob that matches all js files not in a directory
我有一種情況,我需要一個glob模式(使用minimatch
)來匹配不在某個目錄中的所有JavaScript文件。 不幸的是,我正在使用另一個不暴露任何選項的工具(比如ignore
glob),所以它必須是一個單獨的glob來完成這項工作。
例如輸入(它不應該匹配的頂部,但它應該匹配的底部):
docs/foo/thing.js
docs/thing.js
client/docs/foo/thing.js
client/docs/thing.js
src/foo/thing.js
src/thing.js
docs-src/foo/thing.js
docs-src/thing.js
client/docs-src/foo/thing.js
client/docs-src/thing.js
到目前為止,這是我對glob模式的看法:
**/!(docs)/*.js
有了這個我匹配docs/foo/thing.js
和client/docs/foo/thing.js
而不匹配docs-src/thing.js
或client/docs-src/thing.js
。 如果我將我的glob切換到**/!(docs)/**/*.js
client/docs-src/thing.js
然后我可以匹配client/docs-src/thing.js
,但我也匹配client/docs/thing.js
。
我不確定這是可能的,所以我可能需要為我的問題找到另一個解決方案: - /
我想你可能會遇到minimatch(或fnmatch(3)的任何實現)和globstar的限制。 值得注意的是,我所知道的fnmatch的C實現實際上並沒有實現globstar,但是由於fnmatch impls(包括minimatch)服務於他們的globbers的利益,這可能會有所不同。
當你用作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模式。 你將不得不使用排除,或構建一個類似於: {!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js
的glob {!(docs),!(docs)/!(docs),!(docs)/!(docs)/!(docs),!(docs)/!(docs)/!(docs)/!(docs)}/*.js
否則,像x/docs/y/z.js
這樣的路徑將匹配**/!(docs)/**/*.js
x/docs/y/z.js
,說第一個**
沒有匹配, !(docs)
匹配x
,下一個**
與docs/y
匹配,然后*.js
與z.js
匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.