当前位置:网站首页>文件查询匹配神器 【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' ]
此时项目目录为:
更多功能
可参考官网,或留言我来添加哈!
边栏推荐
- 深度学习Course2第二周Optimization Algorithms习题整理
- 企业公众号文章写作方向:如何写出读者认可的优质内容
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- Still struggling with reporting tool selection?To take a look at this
- familiar friend
- 2022-08-01 第八组 曹雨 泛型 枚举
- 三、mysql 存储引擎-建库建表操作
- xctf attack and defense world web master advanced area webshell
- 移动端人脸风格化技术的应用
- 毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
猜你喜欢
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
联邦学习在金融领域的发展和应用
ImportError: `save_weights` requires h5py. Problem solved
(翻译)按钮的对比色引导用户操作的方式
ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot
LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
No more rolls!After joining ByteDance for a week, he ran decisively.
number of solutions to solve a multivariate multi-degree equation
小程序中的多表联合查询
随机推荐
毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
将vim与系统剪贴板的交互使用
小程序容器+自定义插件,可实现混合App快速开发
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
User Experience | How to Measure User Experience?
程序员必备的 “ 摸鱼神器 ” 来了 !
APP special test: traffic test
统计单词数
【移动Web】移动端适配
Graph Theory - Strongly Connected Component Condensation + Topological Sort
企业公众号文章写作方向:如何写出读者认可的优质内容
leetcode 204. Count Primes 计数质数 (Easy)
Deep Learning Course2 Week 2 Optimization Algorithms Exercises
复现gallerycms字符长度限制短域名绕过
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
【C语言实现】整数排序-四种方法,你都会了吗、
feel so stupid
Centos7--MySQL的安装
(Translation) How the contrasting color of the button guides the user's actions