I need to separate the file path from the file name: (some codes did not work in the past) If the path is www.example.com/hi/test/home.html How would I get just www.example.com/hi/test/?
Asked
Active
Viewed 4,124 times
-1
-
Have a look at `split`, `pop` and `join` methods, then try something. – Tigger Oct 29 '16 at 23:32
-
You can check this thread for details: http://stackoverflow.com/questions/19776979/regex-get-all-characters-after-last-slash-in-url – Mo. Oct 29 '16 at 23:33
-
`"www.example.com/hi/test/home.html".substring(0,url.lastIndexOf("/")+1);` – Mamdouh Saeed Oct 29 '16 at 23:41
-
I just did `var fullPath = path + "/";` – Oct 30 '16 at 11:25
1 Answers
4
You can use substring
and lastIndexOf
:
a = "www.example.com/hi/test/home.html";
b = a.substr(0, a.lastIndexOf('/'));
// b = www.example.com/hi/test

Mamdouh Saeed
- 2,302
- 1
- 9
- 11

Jayce444
- 8,725
- 3
- 27
- 43
-
1Don't forget your `var`s, `const`s, or `let`s before your variables. : ) – jmealy Oct 29 '16 at 23:39
-
1to get trailing slash `url.substring(0,url.lastIndexOf("/")+1);` – Mamdouh Saeed Oct 29 '16 at 23:41