This code accepts a path/to/folder or a path/to/folder/ (notice the trailing slash) and extracts the name of the last folder in the string, i.e.:
path = 'path/to/folder'.split('/');
folder = path.pop() || path.pop(); // taking care of trailing slash
// folder == 'folder'
I'm curious, is it possible to turn this into a one-liner? I would appreciate both regex and non-regex answers :)
As a side note, I just realized that my code doesn't know how to handle more than one trailing slash (like a typo) - so I'd appreciate it if you could take that into account as well.
Edit: I'm really hoping to see a non-regex answer
folder = path.split('/').slice(0, path.split('/').indexOf(''));, but this works only in modern browsers.