Open In App

How to trim a file extension from string using JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
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.




<!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.




<!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:


Last Updated : 29 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads