Get directory of a file name in Javascript

前端 未结 9 1530
囚心锁ツ
囚心锁ツ 2021-01-17 07:41

How to get the directory of a file?

For example, I pass in a string

C:\\Program Files\\nant\\bin\\nant.exe

I want a function that r

9条回答
  •  终归单人心
    2021-01-17 08:34

    Use:

    var dirname = filename.match(/(.*)[\/\\]/)[1]||'';
    

    *The answers that are based on lastIndexOf('/') or lastIndexOf('\') are error prone, because path can be "c:\aa/bb\cc/dd".
    (Matthew Flaschen did took this into account, so my answer is a regex alternative)

提交回复
热议问题