omega omega - 1 year ago 139
Javascript Question

How to get last folder name from folder path in javascript?

In javascript/jquery, given a path to a folder like:



"http://www.blah/foo/bar" (this one doesn't have a slash in the end)

How can you extract the name of the last folder? In this case it would be

Is there an easy way through a built in function?


Answer Source

Use the power of the regular expression :

var name = path.match(/([^\/]*)\/*$/)[1]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download