Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
javascript regex to extract paths from config.php string

i like to minify all js-files from a config.php by gulp.

//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' 
));

i read the file by fs.readFile to a string. so far so good.
unfortunately i can't find the correct regex/match to get only the paths between:

c::set('scripts', array(

and

));

anybody knows the right regex?
i am regex newbie. tnx

Update

With the regex from @Ken following the working solution:

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'));
});
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This snippet (with help from regex101.com) outputs the strings - does it meet your needs?

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}`);
    });
}

over 4 years ago · Santiago Trujillo Denunciar

0

Alternative way of achieving the same thing

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(',')
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda