I need a short basename function (one-liner ?) for Javascript:
basename(\"/a/folder/file.a.ext\") -> \"file.a\"
basename(\"/a/folder/file.ext\") -> \"f
Fairly simple using regex:
function basename(input) {
return input.split(/\.[^.]+$/)[0];
}
Explanation:
Matches a single dot character, followed by any character except a dot ([^.]), one or more times (+), tied to the end of the string ($).
It then splits the string based on this matching criteria, and returns the first result (ie everything before the match).
[EDIT] D'oh. Misread the question -- he wants to strip off the path as well. Oh well, this answers half the question anyway.