Skip to content
Related Articles
Open in App
Not now

Related Articles

How to get the file name from full path using JavaScript ?

Improve Article
Save Article
  • Last Updated : 16 Jan, 2023
Improve Article
Save Article

Given a file name that contains the file path also, the task is to get the file name from the full path. There are a few methods to solve this problem which are listed below:

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: This method accepts two parameters as mentioned above and described below:

  • searchVal: This parameter is required. It specifies the value, or regular expression, that is going to replace by the new value.
  • newvalue: This parameter is required. It specifies the value to be replaced with the search value.

Example: This example gets the file name with the help of Regular Expression by using replace() method

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
  
<p id="GFG_UP">
</p>
  
<button onclick="gfg_Run()">
    get File Name
</button>
  
<p id="GFG_DOWN">
</p>
  
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var path = "Path = " +
            "C:\\Documents\\folder\\img\\GFG.jpg";
      
    el_up.innerHTML = path;
              
    function gfg_Run() {
        el_down.innerHTML = path.replace(/^.*[\\\/]/, '');
    }        
</script>

Output:

How to get the file name from full path using JavaScript ?

How to get the file name from full path 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: This method accepts two parameters as mentioned above and described below:

  • separator: This parameter is optional. 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: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.

JavaScript Array pop() Method: This method deletes the last element of an array, and returns deleted element. 

Syntax:

array.pop()

Return value: It returns any type, representing the deleted array item. This item can be a string, number, array, boolean, or any other object types which are allowed in an array.

Example 2: This example gets the file name with the help of repeated split() and pop() method. 

html




<h1 style="color:green;">
    GeeksforGeeks
</h1>
  
<p id="GFG_UP">
</p>
  
<button onclick="gfg_Run()">
    get File Name
</button>
  
<p id="GFG_DOWN">
</p>
  
<script>
    var el_up = document.getElementById("GFG_UP");
    var el_down = document.getElementById("GFG_DOWN");
    var path = "Path = " +
            "C:\\Documents\\folder\\img\\GFG.jpg";
    el_up.innerHTML = path;
              
    function gfg_Run() {
        el_down.innerHTML
            = path.split('\\').pop().split('/').pop();
    }        
</script>

Output:

How to get the file name from full path using JavaScript ?

How to get the file name from full path using JavaScript ?


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!