看代码:

var file1 = "50.xsl";
var file2 = "30.doc";
getFileExtension(file1); //returns xsl
getFileExtension(file2); //returns doc

function getFileExtension(filename) {
    /*TODO*/
}

当前回答

我相信将来会有人缩小和/或优化我的代码。但是,到目前为止,我有200%的信心,我的代码在每一个独特的情况下工作(例如,只有文件名,相对,根相对,和绝对URL的,片段#标签,与查询?字符串,以及任何你可能决定扔给它的东西),完美无缺,精确到极点。

为了证明,请访问:https://projects.jamesandersonjr.com/web/js_projects/get_file_extension_test.php

这是JSFiddle: https://jsfiddle.net/JamesAndersonJr/ffcdd5z3/

不要过于自信,或者自吹自擂,但我还没有看到任何代码块的这个任务(找到“正确的”文件扩展名,在电池不同的函数输入参数),工作得很好。

注意:按照设计,如果给定的输入字符串不存在文件扩展名,它只返回一个空白字符串“”,而不是错误,也不是错误消息。

它有两个参数: 字符串:fileNameOrURL(自解释) 布尔值:showUnixDotFiles(是否显示以点“。”开头的文件)

注(2):如果你喜欢我的代码,一定要把它添加到你的js库中,和/或repo库中,因为我努力完善它,浪费它将是一种耻辱。所以,废话不多说,下面是:

function getFileExtension(fileNameOrURL, showUnixDotFiles)
    {
        /* First, let's declare some preliminary variables we'll need later on. */
        var fileName;
        var fileExt;
        
        /* Now we'll create a hidden anchor ('a') element (Note: No need to append this element to the document). */
        var hiddenLink = document.createElement('a');
        
        /* Just for fun, we'll add a CSS attribute of [ style.display = "none" ]. Remember: You can never be too sure! */
        hiddenLink.style.display = "none";
        
        /* Set the 'href' attribute of the hidden link we just created, to the 'fileNameOrURL' argument received by this function. */
        hiddenLink.setAttribute('href', fileNameOrURL);
        
        /* Now, let's take advantage of the browser's built-in parser, to remove elements from the original 'fileNameOrURL' argument received by this function, without actually modifying our newly created hidden 'anchor' element.*/ 
        fileNameOrURL = fileNameOrURL.replace(hiddenLink.protocol, ""); /* First, let's strip out the protocol, if there is one. */
        fileNameOrURL = fileNameOrURL.replace(hiddenLink.hostname, ""); /* Now, we'll strip out the host-name (i.e. domain-name) if there is one. */
        fileNameOrURL = fileNameOrURL.replace(":" + hiddenLink.port, ""); /* Now finally, we'll strip out the port number, if there is one (Kinda overkill though ;-)). */  
        
        /* Now, we're ready to finish processing the 'fileNameOrURL' variable by removing unnecessary parts, to isolate the file name. */
        
        /* Operations for working with [relative, root-relative, and absolute] URL's ONLY [BEGIN] */ 
        
        /* Break the possible URL at the [ '?' ] and take first part, to shave of the entire query string ( everything after the '?'), if it exist. */
        fileNameOrURL = fileNameOrURL.split('?')[0];

        /* Sometimes URL's don't have query's, but DO have a fragment [ # ](i.e 'reference anchor'), so we should also do the same for the fragment tag [ # ]. */
        fileNameOrURL = fileNameOrURL.split('#')[0];

        /* Now that we have just the URL 'ALONE', Let's remove everything to the last slash in URL, to isolate the file name. */
        fileNameOrURL = fileNameOrURL.substr(1 + fileNameOrURL.lastIndexOf("/"));

        /* Operations for working with [relative, root-relative, and absolute] URL's ONLY [END] */ 

        /* Now, 'fileNameOrURL' should just be 'fileName' */
        fileName = fileNameOrURL;
        
        /* Now, we check if we should show UNIX dot-files, or not. This should be either 'true' or 'false'. */  
        if ( showUnixDotFiles == false )
            {
                /* If not ('false'), we should check if the filename starts with a period (indicating it's a UNIX dot-file). */
                if ( fileName.startsWith(".") )
                    {
                        /* If so, we return a blank string to the function caller. Our job here, is done! */
                        return "";
                    };
            };
        
        /* Now, let's get everything after the period in the filename (i.e. the correct 'file extension'). */
        fileExt = fileName.substr(1 + fileName.lastIndexOf("."));

        /* Now that we've discovered the correct file extension, let's return it to the function caller. */
        return fileExt;
    };

享受吧!不客气!:

其他回答

以下解决方案足够快速和简短,可以用于批量操作并节省额外的字节:

 return fname.slice((fname.lastIndexOf(".") - 1 >>> 0) + 2);

下面是另一个单行非regexp通用解决方案:

 return fname.slice((Math.max(0, fname.lastIndexOf(".")) || Infinity) + 1);

对于没有扩展名(例如myfile)或以开头的名称,两者都可以正常工作。点(例如。htaccess):

 ""                            -->   ""
 "name"                        -->   ""
 "name.txt"                    -->   "txt"
 ".htpasswd"                   -->   ""
 "name.with.many.dots.myext"   -->   "myext"

如果你关心速度,你可以运行基准测试,检查提供的解决方案是否是最快的,而简短的解决方案是非常快的:

短的是如何工作的:

String.lastIndexOf method returns the last position of the substring (i.e. ".") in the given string (i.e. fname). If the substring is not found method returns -1. The "unacceptable" positions of dot in the filename are -1 and 0, which respectively refer to names with no extension (e.g. "name") and to names that start with dot (e.g. ".htaccess"). Zero-fill right shift operator (>>>) if used with zero affects negative numbers transforming -1 to 4294967295 and -2 to 4294967294, which is useful for remaining the filename unchanged in the edge cases (sort of a trick here). String.prototype.slice extracts the part of the filename from the position that was calculated as described. If the position number is more than the length of the string method returns "".


如果你想要更清晰的解决方案,将以同样的方式工作(加上额外的全路径支持),请检查以下扩展版本。这个解决方案比以前的一行程序要慢,但是更容易理解。

function getExtension(path) {
    var basename = path.split(/[\\/]/).pop(),  // extract file name from full path ...
                                               // (supports `\\` and `/` separators)
        pos = basename.lastIndexOf(".");       // get last position of `.`

    if (basename === "" || pos < 1)            // if file name is empty or ...
        return "";                             //  `.` not found (-1) or comes first (0)

    return basename.slice(pos + 1);            // extract extension ignoring `.`
}

console.log( getExtension("/path/to/file.ext") );
// >> "ext"

这三种变体都可以在客户端的任何web浏览器中工作,也可以在服务器端NodeJS代码中使用。

我只是想分享这个。

fileName.slice(fileName.lastIndexOf('.'))

虽然这有一个缺点,即没有扩展名的文件将返回最后一个字符串。 但如果你这样做,这将解决所有问题:

   function getExtention(fileName){
     var i = fileName.lastIndexOf('.');
     if(i === -1 ) return false;
     return fileName.slice(i)
   }

我已经晚了很多个月,但为了简单起见,我使用了这样的东西

var 文件名 = “I.Am.文件名.docx”; var nameLen = fileName.length; var lastDotPos = fileName.lastIndexOf(“.”); var 文件名子 = 假; if(lastDotPos === -1) { 文件名子 = 假; } 还 { 如果希望“.”也保留,请删除 +1 fileNameSub = fileName.substr(lastDotPos + 1, nameLen); } document.getElementById(“showInMe”).innerHTML = fileNameSub; <div id=“showInMe”></div>

return filename.split('.').pop();

编辑:

这是另一个我认为更有效的非正则表达式解决方案:

return filename.substring(filename.lastIndexOf('.')+1, filename.length) || filename;

下面的VisioN的答案可以更好地处理一些极端情况,特别是没有扩展名(。包括Htaccess等)。

它的性能非常好,并且以一种可以说更好的方式处理边缘情况,当没有点或点前没有字符串时,返回""而不是完整的字符串。这是一个精心设计的解决方案,尽管很难阅读。把它放在你的助手库中,然后使用它。

老编辑:

如果遇到没有扩展名的文件,或者没有扩展名的隐藏文件(参见VisioN对Tom的回答的评论),一个更安全的实现应该是这样的

var a = filename.split(".");
if( a.length === 1 || ( a[0] === "" && a.length === 2 ) ) {
    return "";
}
return a.pop();    // feel free to tack .toLowerCase() here if you want

如果a.length为1,则它是一个没有扩展名ie的可见文件。文件

如果一个[0]=== ""和a.length === 2,它是一个隐藏文件,没有扩展名为。htaccess

这应该可以解决稍微复杂一些的情况下的问题。在性能方面,我认为这个解决方案比大多数浏览器中的regex稍微慢一些。然而,对于大多数常见的目的,这段代码应该是完全可用的。

这个简单的解决方案

function extension(filename) {
  var r = /.+\.(.+)$/.exec(filename);
  return r ? r[1] : null;
}

测试

/* tests */ test('cat.gif', 'gif'); test('main.c', 'c'); test('file.with.multiple.dots.zip', 'zip'); test('.htaccess', null); test('noextension.', null); test('noextension', null); test('', null); // test utility function function test(input, expect) { var result = extension(input); if (result === expect) console.log(result, input); else console.error(result, input); } function extension(filename) { var r = /.+\.(.+)$/.exec(filename); return r ? r[1] : null; }