Remove file extension from a String using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Table of Contents

  1. Remove file extension from a string using JavaScript
  2. Remove a file extension from a string in Node.js
  3. Remove a file extension from a String using a regular expression

# Remove file extension from a string using JavaScript

Use the substring() method to remove a file extension from a string.

The substring method will return a new string that consists of the file name without the extension.

index.js
function removeExtension(filename) { return ( filename.substring(0, filename.lastIndexOf('.')) || filename ); } // ๐Ÿ‘‡๏ธ myFile console.log(removeExtension('myFile.jpeg')); // ๐Ÿ‘‡๏ธ myFile console.log(removeExtension('myFile')); // ๐Ÿ‘‡๏ธ /my-folder/myFile console.log(removeExtension('/my-folder/myFile.jpeg'));

remove file extension from string

The code for this article is available on GitHub

The String.substring() method returns a slice of the string from the start index to the excluding end index.

The method takes the following parameters:

NameDescription
start indexThe index of the first character to include in the returned substring
end indexThe index of the first character to exclude from the returned substring

We start at index 0 and go up to, but not including the index of the last dot in the string.

The substring method does not mutate the original string, so you have to save the result in a variable. Strings are immutable in JavaScript.

The String.lastIndexOf() method returns the index of the last occurrence of the specified substring in the string.

If the specified substring (the dot character) is not contained in the string, then lastIndexOf will return -1.

If the lastIndexOf method returns -1, our call to the substring method will return an empty string. This is why we used the logical OR (||) operator to return the passed-in string if the function is called with a filename without an extension.

Note that if you write your code in Node.js, you can use the built-in path module.

# Remove a file extension from a string in Node.js

To remove a file extension from a string in Node.js:

  1. Import the path module.
  2. Pass the string to the path.parse() method.
  3. Access the name property on the returned object.
  4. The name property contains the filename without the extension.
index.js
import path from 'path'; // ๐Ÿ‘‡๏ธ if you use older CommonJS syntax // const path = require('path'); console.log(path.parse('myFile.jpeg').name); // ๐Ÿ‘‰๏ธ "myFile" console.log(path.parse('myFile.jpeg').ext); // ๐Ÿ‘‰๏ธ ".jpeg" console.log(path.parse('myFile').name); // ๐Ÿ‘‰๏ธ "myFile" console.log(path.parse('myFile').ext); // ๐Ÿ‘‰๏ธ "" // ๐Ÿ‘‡๏ธ "myFile" console.log(path.parse('/my-folder/myFile.jpeg').name); // ๐Ÿ‘‡๏ธ ".jpeg" console.log(path.parse('myFolder/myFile.jpeg').ext);

remove file extension from string in node js

The code for this article is available on GitHub

If you use the older CommonJS syntax, use the following import statement instead.

index.js
const path = require('path');

The pat.parse() function takes a string as a parameter and returns an object.

You can access the name property on the object to get the filename without the extension.

# Remove a file extension from a String using a regular expression

Alternatively, you can use a regular expression.

index.js
function removeExtension(filename) { return filename.replace(/\.[^\/.]+$/, ''); } // ๐Ÿ‘‡๏ธ myFile console.log(removeExtension('myFile.jpeg')); // ๐Ÿ‘‡๏ธ myFile console.log(removeExtension('myFile')); // ๐Ÿ‘‡๏ธ /my-folder/myFile console.log(removeExtension('/my-folder/myFile.jpeg'));

remove file extension from string using regular expression

The code for this article is available on GitHub

The forward slashes / / mark the beginning and end of the regular expression.

The \. characters match the dot . character. We had to escape it because it has special meaning in regular expressions.

The square brackets [] are called a character class.

When used inside of the square brackets [], a caret ^ symbol means "not the following". It's basically a negated match.

So we match any character that is not a forward slash and a dot in our character class.

The plus + matches the preceding item 1 or more times and the dollar sign $ matches the end of the input.

In its entirety, the regular expression matches a dot and any character that is not a dot or forward slash at the end of a string.

If you ever need help reading a regular expression, bookmark this regex cheat sheet from MDN. It's by far the best one out there.

The second parameter we passed to the replace method is the replacement for the match - in our case an empty string because we want to remove the file extension.

Which approach you pick is a matter of personal preference. I'd go with using the substring method, because it's easy to read, it's quite intuitive and can be used in the browser and Node.js.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev