Dave's Brain

Browse - programming tips - javascript dirname basename

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(/\/[^\/]*$/,'');
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: