Javascript filename from url

JavaScript: Get filename & file extension from a URL

This short and straightforward article will walk you through a couple of different ways to extract the name and the extension from a URL of a file. All of these approaches will only use vanilla JavaScript. No third-party libraries are required.

Using the split() method

The point here is to use the split() method to split the URL string by the forward slash “/” delimiter. Next, access the last item in the resulting array to get the file name. Use the split() method again to split the file name by the period “.” delimiter. Access the last item in the resulting array to get the file extension.

// a URL to a photo const url = 'https://www.slingacademy.com/some-path/sample-photo.jpg'; // Split the URL by "/" and get the last element using spread operator const [. parts] = url.split('/'); // get the filename const fileName = parts.pop(); // Split the file name by "." const [. segments] = fileName.split('.'); // get the file extension const fileExtension = segments.pop(); // get the name of the file without extension const fileNameWithoutExtension = segments.join('.'); // Log them to the console console.log("File Name:", fileName); console.log("File Name Without Extension:", fileNameWithoutExtension); console.log("File Extension:", fileExtension);
File Name: sample-photo.jpg File Name Without Extension: sample-photo File Extension: jpg

Using regular expressions

You can also use regular expressions to get the filename and file extension from a given URL. The pattern we will use is:

// sample URL const url = 'https://www.slingacademy.com/some-path/my-file.zip'; // The regex to match file name and extension const regex = /([^\/]+)\.([^\/]+)$/i; // Apply the regex to the URL const match = url.match(regex); // Check if there is a match if (match) < // Get the file name from the first capture group const filemaWithoutExtension = match[1]; // Get the file extension from the second capture group const fileExtension = match[2]; // filemname with extension const fileName = filemaWithoutExtension + '.' + fileExtension; // Log the file name and extension console.log('File name:', fileName); console.log('File name without extension:', filemaWithoutExtension) console.log('File extension:', fileExtension); >else < // No match found console.log('No file name or extension found'); >
File name: my-file.zip script.js:23 File name without extension: my-file script.js:24 File extension: zip

You can find more information about regular expressions in this cheat sheet: JavaScript Regular Expressions Cheat Sheet.

Читайте также:  Windows server enable javascript

Happy coding & have a nice day!

Источник

How to get the FileName from Url in JavaScript

www.encodedna.com

A URL or a web address, often has a file name, with few parameters occasionally. Sometimes, we might need to know only the file name in the URL. There’s a simple technique to extract the file name from a url in JavaScript.

We want the file name from the url. But, first we need a url. You can use window.location.href to get the url of the current page or you can simply use the complete web address.

The alert will show the complete url (the web address), which will have the file name and few parameter etc. To get only the file name, I’ll use the split() and pop() functions.

<script> let url = window.location.href; let filename = url.split('/').pop(); alert (filename); </script>

The split() function creates an array of string, after removing all the forward slash (/) from the url string. The pop() function will return the last element, which is file name, from the array. And that’s what we want.

Things get a little tricky, when the url has parameters (also called query strings or variables) and other information. A url with query string may look like this.

https://www.encodedna.com/javascript/practice-ground/default.htm?pg= accordion_menu_using_jquery_css

The above (second) example will not be able to return the file name in this case. Because the split() in the second example, removes the forward slashes only. The query string now have a question mark (?). We need to get rid of this too. Here’s how we can do this.

<script> let url = window.location.href; let filename = url.split('/').pop().split('?')[0]; alert (filename); </script>

Now, it will split, pop and split again, to get the file name. Similarly, the url may have multiple parameters separated by &, it may # and lots of other information. In such cases, you might have to use the split() function multiple times.

<script> var url = window.location.href; var filename = url.split('/').pop().split('?')[0].split('#')[0]; alert (filename); </script>

That’s it. Thanks for reading. ☺

Источник

Get a filename from URL in JavaScript

here in this tutorial, we can see how to get a filename from the URL in javascript.

How to Get a filename from URL in JavaScript

to get a filename from URL in javascript call the following method URL substring then the URL of the last index of / plus 1 we will get the exact filename.

Usually, the file name will be in the last part of the URL, sourcefreeze.com/filename.png here filename.png is the file name first to get the URL we can use window.location.pathname then try to extract the filename using url.substring method by providing url.lastIndexOf(‘/’)+1 .

Please refer to the exact code below to get the filename, here first we are checking URL is valid not if valid then we are getting the correct filename else to return the empty string.

 function getFileName(url) < if (url) < let filename = url.substring(url.lastIndexOf('/') + 1); return filename; >return ""; >

How to get the filename from URL without extension

to get a filename from URL in javascript without extension call the following method URL substring then the URL of the last index of / plus 1 then URL last index of . will remove the extension of the file so we will get the exact filename without the extension.

In some cases, we need to get the filename from the URL without an extension for example sourcefreeze.com/filename.png is a URL we need the filename only without .png, please find the code to get the file name without an extension below.

we are using the same approach to get the file name but just adding the url.lastIndexof(«.») to remove the png or file extension.

let url = "http://sourcefreeze.com/filename.png"; let filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));

How to get the file name from URL using Regex

To get the file name in the javascript using regex, we can use the javascript regex exec method. it will return the array of values which contains the filename, index, and input group details in the array of 0 positions we will get the exact file name.

const fileName = /[^/]*$/.exec('https://sourcefreeze.com/wp-content/uploads/2022/01/source_freeze.png') console.log(fileName[0]) // source_freeze.png console.log(fileName) // will return the filename, index, input group details 

in the first array of 0, we will get the exact file name plus group, index, and input details correspondingly.

How to get the file name from URL without extension using Regex:

The below code is used to get the file name from the URL without extension using the regex method, here we just getting dividing the name / then dividing with . after that get the first part of the array so we will get the exact file name.

let url = "https://sourcefreeze.com/wp-content/uploads/2022/01/source_freeze.png"; let filename = url.match(/([^\/]+)(?=\.\w+$)/)[0]; console.log(filename); 

Please let us know if any questions in the comments.

Источник

Get filename from URL with extension

Get the filename from the URL or path using Javascript:

var url = «http://www.example.com/my_page.html»

Method 2: Regular Expression

But when URL is something like this with query parameters:

var url = «http://www.example.com/my_page.html?id=123&value=456»

Then above methods will return:

In order to remove query parameters from the URL, use:

Query parameters reside in next index.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)

Like this:

Author: Vijaya Sankar N

Leave a Reply Cancel reply

Search the desk

My desk visitors

Get notifications

You can reach me here

Categories

  • Bible (44)
    • Outlines (6)
    • Thoughts (23)
    • Verse Explanation (23)
    • Amazon Web Services (3)
    • Salesforce (42)
      • Marketing Cloud (19)
      • Angular (3)
      • Bootstrap (1)
      • CSS (1)
      • Javascript (5)
      • NodeJS (1)
      • PHP (10)
      • SQL (1)
      • Laravel (7)
      • Keys (1)
      • Ubuntu (2)
      • Windows (2)
      • Presentation (1)
      • TED Talks (24)
      • LAMP (1)

      Top Viewed

      On the desk

      In the shelf

      • June 2023 (6)
      • May 2023 (1)
      • April 2023 (1)
      • March 2023 (1)
      • February 2023 (1)
      • December 2022 (1)
      • May 2022 (1)
      • April 2022 (1)
      • March 2022 (1)
      • February 2022 (1)
      • January 2022 (1)
      • November 2021 (1)
      • May 2021 (7)
      • March 2021 (1)
      • February 2021 (1)
      • January 2021 (1)
      • December 2020 (1)
      • November 2020 (1)
      • October 2020 (1)
      • September 2020 (6)
      • August 2020 (1)
      • July 2020 (3)
      • May 2020 (3)
      • April 2020 (3)
      • March 2020 (1)
      • February 2020 (2)
      • November 2019 (1)
      • October 2019 (2)
      • August 2019 (2)
      • July 2019 (2)
      • May 2019 (1)
      • April 2019 (2)
      • March 2019 (1)
      • February 2019 (2)
      • January 2019 (2)
      • December 2018 (3)
      • November 2018 (4)
      • October 2018 (3)
      • September 2018 (1)
      • August 2018 (1)
      • July 2018 (1)
      • June 2018 (1)
      • May 2018 (4)
      • April 2018 (2)
      • March 2018 (5)
      • February 2018 (3)
      • January 2018 (4)
      • December 2017 (2)
      • November 2017 (1)
      • October 2017 (3)
      • September 2017 (3)
      • August 2017 (13)
      • July 2017 (12)
      • June 2017 (3)
      • May 2017 (5)
      • April 2017 (6)
      • March 2017 (5)
      • February 2017 (7)
      • January 2017 (8)
      • December 2016 (11)
      • November 2016 (4)
      • October 2016 (10)
      • May 2016 (2)
      • April 2016 (1)

      Источник

Оцените статью