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

175
Visualizações
Testing for exact string with Regexp

I'm trying to find the exact match for the following strings;

/test/anyAlphaNumericId123
/test/anyAlphaNumericId123/

That will not match the following

/test/anyAlphaNumericId123/x

I have the following Regex currently, which matches both former cases and not the latter;

/(\/test\/)(.*?)(\/|$)/

But on attempting to call str.match on it, I will also get a result for the latter, as it has a partial match. Can I accomplish this with Regex alone?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can write the pattern as:

^\/test\/[a-zA-Z0-9]+\/?$

The pattern matches:

  • ^ Start of string
  • \/test\/ Match /test/
  • [a-zA-Z0-9]+ Match 1+ times any of the allowed ranges
  • \/? Match an optional /
  • $ End of string

Regex demo

Example using a case insensitive match with the /i flag:

const regex = /^\/test\/[a-z0-9]+\/?$/i;
[
  "/test/anyAlphaNumericId123",
  "/test/anyAlphaNumericId123/",
  "/test/anyAlphaNumericId123/x"
].forEach(s =>
  console.log(regex.test(s) ? `Match: ${s}` : `No match ${s}`)
);

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