Regular expression to remove a file's extension

I am in need of a regular expression that can remove the extension of a filename, returning only the name of the file.

Here are some examples of inputs and outputs:

myfile.png     -> myfile
myfile.png.jpg -> myfile.png

I can obviously do this manually (ie removing everything from the last dot) but I'm sure that there is a regular expression that can do this by itself.

Just for the record, I am doing this in JavaScript


Solution 1:

Just for completeness: How could this be achieved without Regular Expressions?

var input = 'myfile.png';
var output = input.substr(0, input.lastIndexOf('.')) || input;

The || input takes care of the case, where lastIndexOf() provides a -1. You see, it's still a one-liner.

Solution 2:

/(.*)\.[^.]+$/

Result will be in that first capture group. However, it's probably more efficient to just find the position of the rightmost period and then take everything before it, without using regex.

Solution 3:

/^(.+)(\.[^ .]+)?$/

Test cases where this works and others fail:

  • ".htaccess" (leading period)
  • "file" (no file extension)
  • "send to mrs." (no extension, but ends in abbr.)
  • "version 1.2 of project" (no extension, yet still contains a period)

The common thread above is, of course, "malformed" file extensions. But you always have to think about those corner cases. :P

Test cases where this fails:

  • "version 1.2" (no file extension, but "appears" to have one)
  • "name.tar.gz" (if you view this as a "compound extension" and wanted it split into "name" and ".tar.gz")

How to handle these is problematic and best decided on a project-specific basis.

Solution 4:

The regular expression to match the pattern is:

/\.[^.]*$/

It finds a period character (\.), followed by 0 or more characters that are not periods ([^.]*), followed by the end of the string ($).

console.log( 
  "aaa.bbb.ccc".replace(/\.[^.]*$/,'')
)