How to trim a file extension from string using JavaScript ? Last Updated : 29 May, 2019 Comments Improve Suggest changes Like Article Like Report Given a fileName in string format and the task is to trim the file extension from the string using JavaScript. replace() method: This method searches a string for a defined value or a regular expression, and returns a new string with the replaced defined value. Syntax: string.replace(searchVal, newvalue) Parameters: searchVal: It is required parameter. It specifies the value or regular expression, that is going to replace by the new value. newvalue: It is required parameter. It specifies the value to be replaced with the search value. Return value: It returns a new string where the defines value(s) has been replaced by the new value. split() method: This method is used to split a string into an array of substrings and returns the new array. Syntax: string.split(separator, limit) Parameters: separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item). limit: It is optional parameter. It specifies the integer that specifies the number of split items beyond the split limit will be excluded from the array. Return value: It returns a new Array, having the splitted items. JavaScript String slice() method: This method gets parts of a string and returns the extracted parts in a new string. Start and end parameters are used to specify the part of the string to extract. The first character starts from position 0, the second has position 1, and so on. Syntax: string.slice(start, end) Parameters: start: It is required parameter. It specifies the position from where to start the extraction. First character start from position 0. end: It is optional parameter. It specifies the position (excluding it) where to stop the extraction. If not used, slice() selects all characters from the start-position to the end. Return value: It returns a string, representing the extracted part of the string. JavaScript Array join() Method: This method adds the elements of an array into a string, and returns the string. The elements will be separated by a passed separator. The default separator is comma (, ). Syntax: array.join(separator) Parameters: This method accepts single parameter separator which is optional. It specifies the separator to be used. If not used, the elements are separated with a comma Return value: It returns a string, denoting the array values, separated by the defined separator. Example 1: This example gets the file Name by using split(), slice() and join() methods. html <!DOCTYPE HTML> <html> <head> <title> Trim a file extension from a string using JavaScript </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var fName = "fileName.jpg"; el_up.innerHTML = "String = '"+fName + "'"; function gfg_Run() { el_down.innerHTML = fName.split('.').slice(0, -1).join('.'); } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: This example gets the file Name by using RegExp along with replace() method. html <!DOCTYPE HTML> <html> <head> <title> Trim a file extension from a string using JavaScript </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var fName = "fileName.jpg"; el_up.innerHTML = "String = '" + fName + "'"; function gfg_Run() { el_down.innerHTML =fName.replace(/\.[^/.]+$/, "") } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to trim a file extension from string using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to get file extensions using JavaScript? JavaScript provides several methods to extract the file extension from a file name. Below are the three commonly used methods:Letâs see each of these methods one by one with examples.Table of ContentUsing split() and pop() MethodUsing substring() and lastIndexOf() MethodUsing match() Method with Reg 3 min read How to remove time from date using JavaScript ? Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is 2 min read How to Remove Spaces From a String using JavaScript? These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.split() method is used to split a string into multiple sub-strings and return them in the form of an array. The join() method is used to join an array of strings using 2 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to trim a string at beginning or ending in JavaScript? Given a String with unused Spaces, the task is to trim a string at the beginning, ending, or from both sides. Below are the approaches to trim a string at the beginning or ending using JavaScript: Table of ContentTrim a String at the Beginning with stimStart() MethodTrim a String at the End using tr 3 min read How to remove HTML tags from a string using JavaScript ? Removing HTML tags from a string in JavaScript means stripping out the markup elements, leaving only the plain text content. This is typically done to sanitize user input or to extract readable text from HTML code, ensuring no unwanted tags remain in the string.HTML tags come in two forms: opening t 3 min read How to extract extension from a filename using PHP ? In this article, we will see how to extract the filename extension in PHP, along with understanding their implementation through the examples. There are a few different ways to extract the extension from a filename with PHP, which is given below: Using pathinfo() function: This function returns info 2 min read How to remove all Non-ASCII characters from the string using JavaScript ? In this article, we are given a string containing some non-ASCII characters and the task is to remove all non-ASCII characters from the given string. Approaches to remove all Non-ASCII Characters from String: Table of Content Using ASCII values in JavaScript regExUsing Unicode in JavaScript regExUsi 3 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange 2 min read Like