Get directory from a file path or url
filepath.split("/").slice(0,-1).join("/"); // get dir of filepath
- split string into array delimited by "/"
- drop the last element of the array (which would be the file name + extension)
- join the array w/ "/" to generate the directory path
Using path module of node.js:
path.dirname('/this/is/a/path/to/a/file');
returns
'/this/is/a/path/to/a'
Using plain JS, this will work:
var e = '/this/is/a/path/to/a/file.html'
e.split("/").slice(0,-1).join("/") //split to array & remove last element
//result: '/this/is/a/path/to/a'
OR... if you prefer a one liner (using regex):
"/this/is/a/path/to/a/file.html".replace(/(.*?)[^/]*\..*$/,'$1')
//result: '/this/is/a/path/to/a/'
OR... finally, the good old fashioned (and faster):
var e = '/this/is/a/path/to/a/file.html'
e.substr(0, e.lastIndexOf("/"))
//result: '/this/is/a/path/to/a'
I think you're looking for path.dirname