Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!