为什么这个节点脚本没有过滤

时间:2017-02-24 13:45:17

标签: javascript regex node.js

我正在编写一个节点脚本,该脚本应该将目录和扩展名称作为args,并记录该目录中以该扩展名结尾的所有文件。听起来很简单,但我的脚本返回一个空数组:

var fs = require('fs');
var ref = process.argv[2];
var extension = process.argv[3];
var files = [];
var dir = fs.readdir(ref, function(err, list){
    if (err) throw err;    
    var filterRx = new RegExp("\." + extension + "$");
    files = list.filter(function(val){
        return filterRx.test(val);
    });


});
console.log(files);

任何人都可以帮助我理解为什么这不能按预期工作吗?

编辑:例如, 给定一个包含以下文件的目录(在回调中由console.log(list)给出):

   "[ 'CHANGELOG.md',"                 !=    "LICENCE.md"                       
   "  'LICENCE.md',"                   !=    "README.md"                        
   "  'README.md',"                    !=    ""                                 
   "  'dat',"                          !=                                       
   "  'data.dat',"                     !=                                       
   "  'data.json',"                    !=                                       
   "  'learnyounode.dat',"             !=                                       
   "  'learnyounode.sql',"             !=                                       
   "  'learnyounode.txt',"             !=                                       
   "  'md',"                           !=                                       
   "  'w00t.dat',"                     !=                                       
   "  'w00t.txt',"                     !=                                       
   "  'words.dat',"                    !=                                       
   "  'wrrrrongdat' ]"        

通过第二个参数"md",我应该得到" CHANGELOG.md"," LICENSE.md"等

我只得到[]:/

0 个答案:

没有答案