Regex for extracting filename from path

前端 未结 17 542
时光取名叫无心
时光取名叫无心 2020-12-03 01:02

I need to extract just the filename (no file extension) from the following path....

\\\\my-local-server\\path\\to\\this_file may_contain-any&character.pdf<

17条回答
  •  有刺的猬
    2020-12-03 01:35

    Here's a slight modification to Angelo's excellent answer that allows for spaces in the path, filename and extension as well as missing parts:

    function parsePath (path) {
        var parts = (/(\w?\:?\\?[\w\-_ \\]*\\+)?([\w-_ ]+)?(\.[\w-_ ]+)?/gi).exec(path);
        return {
            path: parts[0] || "",
            folder: parts[1] || "",
            name: parts[2] || "",
            extension: parts[3] || "",
        };
    }
    

提交回复
热议问题