Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda