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

176
Visualizações
Regex to get substring between first and last occurence

Assume there is the string

just/the/path/to/file.txt

I need to get the part between the first and the last slash: the/path/to

I came up with this regex: /^(.*?).([^\/]*)$/, but this gives me everything in front of the last slash.

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

0

Don't use [^/]*, since that won't match anything that contains a slash. Just use .* to match anything:

/(.*?)\/(.*)\/(.*)/

Group 1 = just, Group 2 = the/path/to and Group 3 = file.txt.

about 4 years ago · Juan Pablo Isaza Relatório

0

The regex should be \/(.*)\/. You can check my below demo:

const regex = /\/(.*)\//;
const str = `just/the/path/to/file.txt`;
let m;

if ((m = regex.exec(str)) !== null) {
    console.log(m[1]);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

This regex expression will do the trick

const str = "/the/path/to/the/peace";
console.log(str.replace(/[^\/]*\/(.*)\/[^\/]*/, "$1"));

[^\/]*\/(.*)\/[^\/]*

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