Dave's Brain

Browse - Programming Tips - How about a dirname() and basename() for javaScript?

Date: 2011feb4
Language: javaScript

Q.  How about a dirname() and basename() for javaScript?

A.  The approach is the same in both - remove the characters we don't want.

function basename(path) { return path.replace(/.*\//, ''); } function dirname(path) { return path.replace(/\/[^\/]*$/,''); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.