• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

108
Vistas
Filtering files in directory with regex.match using Puppeteer

I'm having issues with regex.match not matching filenames which do match when I test them individually in an online checker https://regex101.com

Can anyone spot the problem in code below?

Q: Should I be using regex.test instead of match? If yes, how do I create the regex when it contains variables?

It should match all files starting with: ES_(Stay) True - Lars Eriksson

List of files and directory in path found by fs.readdirSync:

.DS_Store
ES_(Stay) True - Lars Eriksson (22).mp3
ES_(Stay) True - Lars Eriksson (22).mp3.crdownload
ES_(Stay) True - Lars Eriksson.mp3
ES_(Stay) True - Lars Eriksson.mp3.crdownload
Other - File (22).mp3
Other - File (22).mp3.crdownload
Other - File.crdownload
Other - File.mp3
originals

The regex converts to:

/^(ES_\(Stay\) True - Lars Eriksson(?: \([0-9]+\))?.mp3(?:.crdownload?)?)$/

Puppeteer script:

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {

function escapeRegex(string) {
    return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

let path = '/path/to/files/';
let title = 'ES_(Stay) True';
let artist = 'Lars Eriksson';

title = escapeRegex(title);
artist = escapeRegex(artist);

let regex = `/^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$/`;
console.log(regex);

fs.readdirSync(path)
    .filter(f => {
        regex.match();
    })
    .map(f => {
        console.log(f);
    });

})();
about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think to convert string to regex you should use RegExp() not just use it as string for example

let regex = new RegExp(`^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$`, 'gi');
console.log(regex);
  • also you are used regex.match() what do you expect to match when there's nothing to match you are trying to match the regex with nothing it's should to be
f.match(regex)

your code should to be like that

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {

function escapeRegex(string) {
    return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}

let path = '/path/to/files/';
let title = 'ES_(Stay) True';
let artist = 'Lars Eriksson';

title = escapeRegex(title);
artist = escapeRegex(artist);

let regex = new RegExp(`^(${title} - ${artist}(?: \\([0-9]+\\))?\.mp3(?:\.crdownload?)?)$`, 'gi');
console.log(regex);

let file = fs.readdirSync(path),
matched = file.filter(f => f.match(regex))
console.log(matched)
})();

Result

0: "ES_(Stay) True - Lars Eriksson (22).mp3"
1: "ES_(Stay) True - Lars Eriksson (22).mp3.crdownload"
2: "ES_(Stay) True - Lars Eriksson.mp3"
3: "ES_(Stay) True - Lars Eriksson.mp3.crdownload"
about 3 years ago · Juan Pablo Isaza 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda