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
What is the fastest way to extract all the subpaths from a path using regex

For a given path, for example - abc/def/hijkl/mno.txt how to extract all the paths like below

abc
abc/def
abc/def/hijkl
abc/def/hijkl/mno.txt

Like an array with actual path and all the sub paths till the root path.

I've tried the getting this with regex but I do not find a way to match every occurrence of / from the beginning of the string.

Below is the regex I've tried

(.*?)?\/+

Is there any way to get this output with regex in javascript

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

0

You don't need Regexp for this.

You can just split your string into an array using split, then reconstitute slices of that array back into paths using join.

const path = "abc/def/hijkl/mno.txt";

const segments = path.split('/');

const paths = segments.map((s, i) => segments.slice(0, i + 1).join('/'))

console.log(paths)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can split your string by / and append the array list as you desire:

Example:

const str = `abc/def/hijkl/mno.txt`;

let array = str.split('/');

res=""
len=array.length;
for (let i = 0; i < len; i++) {
   res+=array[i];
   console.log(res);
   if(i<len-1)
   res+="/";

}

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