用于从config.php字符串提取路径的javascript正则表达式

用于从config.php字符串提取路径的javascript正则表达式,javascript,php,regex,gulp,Javascript,Php,Regex,Gulp,我喜欢通过gulp缩小config.php中的所有js文件 //config.php c::set('myvar', true); c::set('styles', [ 'test1.css', 'test2.css' ]); c::set('scripts', array( 'node_modules/abc.min.js', 'node_modules/def.js', 'assets/js/xyz.js' )); 我通过fs.readFile将文件读取为字符串。

我喜欢通过gulp缩小config.php中的所有js文件

//config.php
c::set('myvar', true);

c::set('styles', [
  'test1.css',
  'test2.css'
]);

c::set('scripts', array(
  'node_modules/abc.min.js',
  'node_modules/def.js',
  'assets/js/xyz.js' 
));
我通过fs.readFile将文件读取为字符串。到目前为止还不错。
很遗憾,我找不到正确的regex/match,无法仅获取以下路径之间的路径:

c::set('scripts',数组(

)))

有人知道正确的正则表达式吗?
我是regex新手。 tnx

更新 使用@Ken中的正则表达式,遵循以下工作解决方案:

var gulp = require('gulp'),
    fs = require("fs"),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify');

var jsfromconfigphp = [];

gulp.task('get-js-by-config-php', function(done) {
  const regex = /\s+(\'[\w\/\.]+\.js\')/gi;
  let m;
  fs.readFile('site/config/config.php', {encoding: 'utf-8', flag: 'rs'}, function(e, data) {
    if (e) {
      return console.log(e);
    }
    while ((m = regex.exec(data)) !== null) {
      // This is necessary to avoid infinite loops with zero-width matches
      if (m.index === regex.lastIndex) {
        regex.lastIndex++;
      }
      // The result can be accessed through the `m`-variable.
      m.forEach((match, index) => {
        if(index === 1) {
          console.log(`Found match, group ${index}: ${match}`);
          jsfromconfigphp.push(match.slice(1, -1));
        }
      });
    }
    done();
  });
});

// wait for get-js-by-config-php is done
gulp.task('build-js', ['get-js-by-config-php'], function() {
  return gulp.src(jsfromconfigphp)
  .pipe(concat('main.min.js'))
  .pipe(uglify({
    compress: {
      drop_console: true
    }
  }))
  .pipe(gulp.dest('assets/js'));
});
这个代码段(在regex101.com的帮助下)输出字符串-它满足您的需要吗

const regex=/\s+(\'[\w\/]+\.js\')/gi;
const str=`c::set('myvar',true);
c::set('styles'[
“test1.css”,
“test2.css”
]);
c::set('scripts',数组(
“node_modules/abc.js”,
'assets/js/xyz.js'
));`;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});
}
此代码段(在regex101.com的帮助下)输出字符串-它满足您的需要吗

const regex=/\s+(\'[\w\/]+\.js\')/gi;
const str=`c::set('myvar',true);
c::set('styles'[
“test1.css”,
“test2.css”
]);
c::set('scripts',数组(
“node_modules/abc.js”,
'assets/js/xyz.js'
));`;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});

}
实现同样目标的替代方法

var path = "c::set('scripts', array('node_modules/abc.js','assets/js/xyz.js'));";
path = path.replace("c::set('scripts', array(",'')
path = path.replace('));','')
path.replace(/["']/g, "").split(',')

实现同样目标的替代方法

var path = "c::set('scripts', array('node_modules/abc.js','assets/js/xyz.js'));";
path = path.replace("c::set('scripts', array(",'')
path = path.replace('));','')
path.replace(/["']/g, "").split(',')


类似于
fileContent.match(/c::set\(['”])scripts\1\s*array\([^)]*)/)[2]
?@Thomas tnx,但TypeError:无法读取null的属性“2”,它使用@Ken H的总解和正则表达式更新了我的问题。类似于
fileContent.match(/c::set\(['”])scripts\1\s*array\([^)]/)[2]
?@Thomas tnx,但TypeError:无法读取null的属性'2',使用@Ken H.tnx中的完整解决方案和正则表达式更新了我的问题,但是config.php中有更多的设置,因此我想替换以下内容);'这是个问题。我已经更新了上面的代码.tnx,但是config.php中还有很多设置,所以我想替换下面的“);”这是个问题。我已经更新了上面的代码。我已经更新了上面的config.php-example,使用了比js部分更多的设置。您能否解释一下,如何减少脚本部分的输出?以及为什么我在组0和组1中获得相同的匹配。tnx@dennisbaum-不完全一样。整行的匹配项(“组0”)有空格,而表达式括号部分(“组1”)的匹配项只有带引号的字符串。我不是100%清楚您需要什么,但这是一种逐行获取或仅获取每行引用的文件名的方法,并以适合您需要的方式输出。我希望它能在某种程度上对您有所帮助。我刚刚注意到您的其他编辑-将
js
添加到表达式应该很简单-我将编辑post.Tnx Ken,这很有效,但在我再次更新config.php之后,例如添加“node\u modules/abc.min.js”。不幸的是,这不匹配。。。因为“.min.js”。如何获得.min?我更新了上面的config.php-example,使用了比js部分更多的设置。您能否解释一下,如何减少脚本部分的输出?以及为什么我在组0和组1中获得相同的匹配。tnx@dennisbaum-不完全一样。整行的匹配项(“组0”)有空格,而表达式括号部分(“组1”)的匹配项只有带引号的字符串。我不是100%清楚您需要什么,但这是一种逐行获取或仅获取每行引用的文件名的方法,并以适合您需要的方式输出。我希望它能在某种程度上对您有所帮助。我刚刚注意到您的其他编辑-将
js
添加到表达式应该很简单-我将编辑post.Tnx Ken,这很有效,但在我再次更新config.php之后,例如添加“node\u modules/abc.min.js”。不幸的是,这不匹配。。。因为“.min.js”。如何获得.min?