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

更多功能
可参考官网,或留言我来添加哈!
边栏推荐
猜你喜欢

JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method

第一讲 测试知多少

No more rolls!After joining ByteDance for a week, he ran decisively.

移动端人脸风格化技术的应用

小程序容器+自定义插件,可实现混合App快速开发

_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined

小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告

关于ETL的两种架构(ETL架构和ELT架构)

Still struggling with reporting tool selection?To take a look at this

【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
随机推荐
SAP Spartacus Accessibility E2E 端到端测试
The must-have "fishing artifact" for programmers is here!
Still struggling with reporting tool selection?To take a look at this
PHP算法之电话号码的字母组合
工程建筑行业数据中台指标分析
Safe fifth after-school exercise
企业公众号文章写作方向:如何写出读者认可的优质内容
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
SOM网络1:原理讲解
xss相关知识点以及从 XSS Payload 学习浏览器解码
render-props and higher order components
第一讲 测试知多少
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
leetcode 204. Count Primes 计数质数 (Easy)
xctf attack and defense world web master advanced area web2
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
Flutter基础学习(一)Dart语言入门
【开源】Sentinel高性能高可用集群限流解决方案
求解多元多次方程解的个数
【C语言】猜数字小游戏