How to trim a file extension from a String in JavaScript?

前端 未结 23 2129
醉酒成梦
醉酒成梦 2020-11-30 17:21

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let\'s assume the file nam

23条回答
  •  广开言路
    2020-11-30 17:46

    The accepted answer strips the last extension part only (.jpeg), which might be a good choice in most cases.

    I once had to strip all extensions (.tar.gz) and the file names were restricted to not contain dots (so 2015-01-01.backup.tar would not be a problem):

    var name = "2015-01-01_backup.tar.gz";
    name.replace(/(\.[^/.]+)+$/, "");
    

提交回复
热议问题