How can I get file extensions with JavaScript?

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

    There's also a simple approach using ES6 destructuring:

    const path = 'hello.world.txt'
    const [extension, ...nameParts] = path.split('.').reverse();
    console.log('extension:', extension);

提交回复
热议问题