I need a short basename function (one-liner ?) for Javascript:
basename("/a/folder/file.a.ext") -> "file.a" basename("/a/folder/file.ext") -> "file" basename("/a/folder/file") -> "file"
That should strip the path and any extension.
Update: For dot at the beginning would be nice to treat as "special" files
basename("/a/folder/.file.a.ext") -> ".file.a" basename("/a/folder/.file.ext") -> ".file" basename("/a/folder/.file") -> ".file" # empty is Ok basename("/a/folder/.fil") -> ".fil" # empty is Ok basename("/a/folder/.file..a..") -> # does'nt matter
The basename() function returns the filename from a path.
The path. basename() method returns the filename part of a file path.
In NodeJS, __filename. split(/\|//). pop() returns just the file name from the absolute file path on any OS platform.
function basename(path) { return path.split('/').reverse()[0]; }
Breaks up the path into component directories and filename then returns the last piece (the filename) which is the last element of the array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With