当前位置:网站首页>文件查询匹配神器 【glob.js】 实用教程
文件查询匹配神器 【glob.js】 实用教程
2022-08-01 22:18:00 【朝阳39】
glob 官网
https://www.npmjs.com/package/glob
创建 glob 的演示项目
- 新建文件夹 globDemo
- 在 globDemo文件夹中打开命令行,执行
npm init -y
会自动生成 package.json
- 安装 glob
cnpm i glob
无 cnpm 的朋友先执行 npm i cnpm
- 在package.json中添加项目启动脚本
"start": "node index.js",
- 在 globDemo文件夹中新建文件 index.js 内容为
let glob = require("glob");
glob("*.js", {
}, function (er, files) {
console.log(files);
});
- 启动项目
npm run start
得到结果
[ 'index.js' ]
导入 glob
let glob = require("glob");
使用 glob
查找当前目录指定后缀的文件
如查找 js 文件
glob("*.js", {
}, function (er, files) {
console.log(files);
});
*
代表任意文件名- files 为匹配到的文件名构成的数组
[ 'index.js' ]
若在 globDemo 文件夹中新建文件 test.js,则结果为
[ 'index.js', 'test.js' ]
查找所有目录下指定后缀的文件
glob("**/*.js", {
}, function (er, files) {
console.log(files);
});
**/
可匹配任意目录
得到
可见将 node_modules 中的 js 文件全部匹配到了!
过滤掉不需要的文件
- 将不需要的文件目录或文件名添加到 ignore 数组中即可。
在 globDemo 文件夹中新建文件夹 src,在文件夹 src 内新建 page.js
glob(
"**/*.js",
{
ignore: ["node_modules/**", "test.js"],
},
function (er, files) {
console.log(files);
}
);
得到
[ 'index.js', 'src/page.js' ]
此时项目目录为:
更多功能
可参考官网,或留言我来添加哈!
边栏推荐
- Getting Started Database Days4
- 03、GO语言变量定义、函数
- 找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
- SAP Spartacus Accessibility E2E 端到端测试
- Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
- 图论——强连通分量缩点+拓扑排序
- feel so stupid
- number of solutions to solve a multivariate multi-degree equation
- 游戏元宇宙发展趋势展望分析
- User Experience | How to Measure User Experience?
猜你喜欢
(翻译)按钮的对比色引导用户操作的方式
[ASM] Bytecode Operation MethodWriter
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
递归(各经典例题分析)
xctf attack and defense world web master advanced area web2
(Translation) How the contrasting color of the button guides the user's actions
【开源】Sentinel高性能高可用集群限流解决方案
【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读
xctf攻防世界 Web高手进阶区 web2
程序员必备的 “ 摸鱼神器 ” 来了 !
随机推荐
漫长的投资生涯
(Translation) How the contrasting color of the button guides the user's actions
SOM Network 2: Implementation of the Code
1. @Component注解的原理剖析
图论——强连通分量缩点+拓扑排序
【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读
熟悉的朋友
Kubernetes Scheduler全解析
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
第一讲 测试知多少
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
2022 edition of MySQL tutorial, top collection good, take your time
用户体验 | 如何度量用户体验?
xctf攻防世界 Web高手进阶区 webshell
Flutter基础学习(一)Dart语言入门
一种灵活的智能合约协作方式
Recycling rental system 100% open source without encryption Mall + recycling + rental
User Experience | How to Measure User Experience?
NgRx Store createSelector 的单步调试和源代码分析
PHP算法之最接近的三数之和