使用 nodeJs 在 js 文件中查找所有 类

Find all classes in a js file with nodeJs

我想在一个js文件中找到所有类,然后用nodeJs把它添加到一个数组中。 现在我在控制台中 //opened 但我想要 //opened, carambar (all results)

查看我的代码:

var fs = require('fs');
var regExp = /addClass\([\s'"]*(.*?)[\s'"]*\)/gm;
var res;
var file, match;
fs.readFile('assets/js/src/menu.js', (err, data) => {
    if (err) throw err;
    file = ''+data;
    match = regExp.exec(file);
    console.log(file);
    console.log('match: '+match[1],' type: '+typeof match, 'match length '+match.length);
});

以及我要查找的文件:

if (matchMedia('(max-width: 1023px)').matches) {
    //console.log("under 1023px");
    // Mega Menu Mobile
    // Open main-menu
    jQuery(".button__menu-open").on("click", function(e){
        e.preventDefault();
        jQuery(".menu__mobile").addClass("opened");
        jQuery(".menu__mobile").addClass("carambar");
        jQuery("html, body").addClass("menu-mobile--active");
    });
    // Close menu
    jQuery(".button__menu-close").on("click", function(e){
        e.preventDefault();
        jQuery(".menu__mobile").removeClass("opened");
        jQuery("html, body").removeClass("menu-mobile--active");
    });

}

谢谢你

使用 while(match) 查找所有匹配项

var fs = require('fs');
var regExp = /addClass\([\s'"]*(.*?)[\s'"]*\)/gm;
var res;
var file, match;
fs.readFile('b.js', (err, data) => {
   if (err) throw err;
   file = ''+data;
   while(match = regExp.exec(file)){
     console.log('match: '+match[1],' type: '+typeof match, 'match length '+match.length);
   }
});

哪个显示器: 匹配:打开类型:对象匹配长度 2 匹配:carambar 类型:对象匹配长度 2 match: menu-mobile--active type: object 匹配长度 2