How can I get file extensions with JavaScript?

后端 未结 30 2223
终归单人心
终归单人心 2020-11-22 09:37

See code:

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

function getFileExt         


        
30条回答
  •  情话喂你
    2020-11-22 10:06

    i just wanted to share this.

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

    although this has a downfall that files with no extension will return last string. but if you do so this will fix every thing :

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

提交回复
热议问题