How can I get file extensions with JavaScript?

后端 未结 30 1842
终归单人心
终归单人心 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:02

    var file = "hello.txt";
    var ext = (function(file, lio) { 
      return lio === -1 ? undefined : file.substring(lio+1); 
    })(file, file.lastIndexOf("."));
    
    // hello.txt -> txt
    // hello.dolly.txt -> txt
    // hello -> undefined
    // .hello -> hello
    

提交回复
热议问题