A list of files in a directory where the file name matches the regular expression in JS

I want to get all the file names in a directory that matches a specific pattern. I found out that I can accomplish this by following these steps:

var fs = require('fs');

fs.readdir( dir, function(err, list) {
    if(err)
        throw err;
    var regex = new RegExp(".*");
    list.forEach( function(item) {
    if( regex.test(item) ) 
       console.log(item);
    }); 
});

I was wondering if there is another possibility to do this without using a loop, for example. passing regex up fs.readdir(..).
Is this somehow possible, or do I need to use a loop?

+3
source share
2 answers

npm NPM. glob NPM. glob, , .

+1

fs.readdir . readdir(3), .

, . , , , , fs.readdir .

+1

All Articles