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

270
Visualizações
path-to-regexp not matching when passing arguments as variables

I've run into a odd problem. When I try to test if path-to-regexp will match a given path I will not get a result when expected, if I pass the input string from a variable. But if I hard-code the string directly (i.e. the very same string that is being printed to the console when printing the variable) it works?

Any idea what might be going on here?

I build a little prototype you can try out

And here is the code example:

import { match } from "path-to-regexp";
const esc = require("./escapeRegexInPath");

const pattern = new RegExp("[0-9]*");

// Does the pattern match "123"
console.log({ patternTest: pattern.test("123") });
// => {patternTest: true}

// The escape function will escape special characters
const escapedRegex = esc(pattern);
console.log({ escapedRegex });
// => {escapedRegex: "[0-9]\*"}

// This matcher is passed the escaped regex as a variable
const matcher = match(`:entityId(${escapedRegex})`);

// And it fails to match anything with input "123"
console.log({ matcher: matcher("123") });
// {matcher: false}

// This matcher is passed a hardcoded regex
// And it succesfully matches with input "123"
const matcher2 = match(`:entityId([0-9]\*)`);
console.log({ matcher2: matcher2("123") });
// => {matcher2: Object}
about 4 years ago · Juan Pablo Isaza
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