For my next stuff I needed the Javascript equivalents of PHP functions basename() and dirname(). Nothing genius:
-
function basename(path) {
-
return path.replace(/\\/g,'/').replace( /.*\//, '' );
-
}
-
-
function dirname(path) {
-
return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');;
-
}
It obviously supports paths like /home/ozh/stuff.php, http://site.com/file.ext or double backslashed Win32 paths such as C:\\dir\\document.txt.
Edit: Mister Ze in comments points out to PHP.JS, an ongoing project aiming to port PHP to Javascript. Some really neat stuff there.
commented, on 28/Apr/08 at 12:07 am # :
You can find many implementation of php functions in javascript here:
http://kevin.vanzonneveld.net/techblog/article/phpjs_licensing/
basename() is implemented in php.JS project but no dirname(), propose your code :)
said, on 28/Apr/08 at 12:40 am # :
Ze » cool ! :)