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

前端 未结 23 2031
醉酒成梦
醉酒成梦 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条回答
  •  Happy的楠姐
    2020-11-30 17:29

    This is where regular expressions come in handy! Javascript's .replace() method will take a regular expression, and you can utilize that to accomplish what you want:

    // assuming var x = filename.jpg or some extension
    x = x.replace(/(.*)\.[^.]+$/, "$1");
    

提交回复
热议问题