How to get basename of a file name?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wild Pete

    How to get basename of a file name?

    Is there an equivalent to a "basename" in javascript
    that returns the last part of a file name?
    For example, given "C:\My Documents\test. txt" and
    "/usr/local/test.txt", both should return "test.txt".
    Any help would be appreciated.
  • Ivo

    #2
    Re: How to get basename of a file name?

    "Wild Pete" wrote[color=blue]
    > Is there an equivalent to a "basename" in javascript
    > that returns the last part of a file name?
    > For example, given "C:\My Documents\test. txt" and
    > "/usr/local/test.txt", both should return "test.txt".[/color]

    Backslashes in strings should be escaped with another backslash, so
    use "C:\\My Documents\\test .txt" instead. Then run this regex:

    var a="/usr/local/test.txt";
    var b=a.match(/[\/|\\]([^\\\/]+)$/);
    alert( b[1] );

    --Iv




    Comment

    Working...