Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda