Posted By

jatkins on 08/10/11


Tagged

file path String name filename manipulation


Versions (?)

Get filename from path


 / Published in: JavaScript
 

Released into the public domain. Useful for getting just the filename (and extension) from the value of an <input>.

  1. function getFilenameFromPath(filePath) {
  2. lastBackslashIndex = filePath.lastIndexOf('\\');
  3. lastForwardSlashIndex = filePath.lastIndexOf('/');
  4. lastSlashIndex = lastBackslashIndex > lastForwardSlashIndex ? lastBackslashIndex : lastForwardSlashIndex;
  5. return filePath.substring(lastSlashIndex == -1 ? 0 : lastSlashIndex + 1);
  6. }

Report this snippet  

You need to login to post a comment.