me gusta minificar todos los archivos js de un config.php por trago.
//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' )); leí el archivo por fs.readFile en una cadena. Hasta ahora, todo bien.
desafortunadamente no puedo encontrar la expresión regular/coincidencia correcta para obtener solo las rutas entre:
c::set('guiones', matriz(
y
));
alguien sabe la expresión regular correcta?
Soy novato en expresiones regulares. gracias
Con la expresión regular de @Ken siguiendo la solución de trabajo:
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')); });Este fragmento (con la ayuda de regex101.com) genera las cadenas: ¿satisface sus necesidades?
const regex = /\s+(\'[\w\/]+\.js\')/gi; const str = `c::set('myvar', true); c::set('styles', [ 'test1.css', 'test2.css' ]); c::set('scripts', array( 'node_modules/abc.js', 'assets/js/xyz.js' ));`; let m; while ((m = regex.exec(str)) !== 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, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); }Manera alternativa de lograr lo mismo.
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(',')