How can I get file extensions with JavaScript?

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

    A one line solution that will also account for query params and any characters in url.

    string.match(/(.*)\??/i).shift().replace(/\?.*/, '').split('.').pop()
    
    // Example
    // some.url.com/with.in/&ot.s/files/file.jpg?spec=1&.ext=jpg
    // jpg
    

提交回复
热议问题