Wednesday, August 12, 2009

Get a Filename from a Path in Javascript

Here's a quick Javascript function that will parse the filename from a path using regex. It looks for any letter or digit, hyphen, or underscore followed by a dot (.) followed by a letters or numbers for the extension.

function getFileName(path) {
return path.match(/[-_\w]+[.][\w]+$/i)[0];
}

No comments: