当前位置:网站首页>文件查询匹配神器 【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' ]
此时项目目录为:

更多功能
可参考官网,或留言我来添加哈!
边栏推荐
- Prufer sequence
- 【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
- 解决 win10 下 ISE14.7的 iMPACT 崩溃问题 - FPGA 笔记
- APP special test: traffic test
- Shell programming conditional statement
- 【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
- 基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
- Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
- (*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
猜你喜欢

Centos7--MySQL的安装

Prufer序列

高等代数_证明_矩阵的任意特征值的代数重数大于等于其几何重数

小程序中的多表联合查询

xctf attack and defense world web master advanced area web2

Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen

Kubernetes Scheduler全解析

APP专项测试:流量测试

SOM Network 2: Implementation of the Code

KMP 字符串匹配问题
随机推荐
SOM网络2: 代码的实现
第3讲:MySQL数据库中常见的几种表字段数据类型
PHP算法之电话号码的字母组合
more grown, more lonely
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
Implementation principle of VGUgarbage collector (garbage collector)
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
13、学习MySQL 分组
xctf attack and defense world web master advanced area web2
今年的很美味
教你VSCode如何快速对齐代码、格式化代码
SAP ABAP OData 服务如何支持删除(Delete)操作试读版
PHP算法之最接近的三数之和
SAP Spartacus NgExpressEngineDecorator 的工作原理
Go 微服务开发框架DMicro的设计思路
How to add a game character to a UE4 scene
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
Safe fifth after-school exercise
【C语言实现】整数排序-四种方法,你都会了吗、
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)