Get the name of the Image in the URL

Asked

Viewed 1,209 times

6

I need to get the name of the image in the url.

/data/data/com.intel.appx.IZICondominios.xwalk15/files/_pictures/picture_006.jpg

I need you to come back:

picture_006.jpg

The URL template is not fixed, each situation can change. It can have more or less folders.

3 answers

4

An example using the method lastIndexOf()

url = '/data/data/com.intel.appx.IZICondominios.xwalk15/files/_pictures/picture_006.jpg';
var filename = url.substring(url.lastIndexOf('/')+1);
console.log(filename);
  • Solved my problem! Too bad that answer is last placed on the page. I tested all solutions until I get to that which is the simplest.

3

If your image name always has that same number of characters, you can do this:

var url = "/data/data/com.intel.appx.IZICondominios.xwalk15/files/_pictures/picture_006.jpg";

document.write(url.substring(url.length-15, url.length));

But if whoever has the same number of characters is the rest of your url, you can do so:

   var url = "/data/data/com.intel.appx.IZICondominios.xwalk15/files/_pictures/picture_006.jpg";

document.write(url.substring(65, url.length));

But if both are fickle:

var url = "/data/data/com.intel.appx.IZICondominios.xwalk15/files/_pictures/picture_006.jpg";
var n;
for(var i = url.length; i > 0; i--){
	if(url.charAt(i) == "/"){
  	n = i;
		break;
	}
}

document.write(url.substring(n+1, url.length));

  • Hello, thanks for the reply. I forgot to mention one thing. The URL is not fixed. It can be different in every situation.

  • But does the image name size also change? For example, there are times when it is "picture_006.jpg" and there are times when it is "image1.jpg"? Or it is always in this formatting "picture_005.jpg" or "picture_190.jpg"?

  • I’m currently doing the standard part picture_001.jpg, picture_002.jpg, ..., but will have a part yet that I will create that will have the indefinite file name.

  • I think that the correct thing would be to do without following the pattern of the name and instead a random name. picture_006.jpg, image1.jpg, tiago.png, ...

  • I edited with a solution that fits any url size and any image name size. But the name should be at the end of the url.

  • See if it fits.

Show 1 more comment

2


This problem has two parts:

  • read the url
  • extract file name

read the url:

If you don’t already have the url in a string you can use location.pathname or even location.href. This will give you a string that you can use in the next step

extract file name

You can do this with Regex or with .split.

Using regex the rule you are looking for is a string that is at the end of the url (using location.pathname) and containing .jpg. You can do it like this (example):

/([^\/\\]+.jpg)/

Using .split simply remove the last element from the array split generates by breaking the string with str.split(/[\/\\]/).

Examples:

  • regex

Example that logs to console if not found...

var url = location.pathname;
var match = url.match(/[^\/\\]+.jpg/);
if (!match) console.log('não encontrou...');
else alert(match[0]);
  • split

Example that logs to console if not found...

var url = location.pathname;
var partes = url.split(/[\/\\]/);
var img = partes.pop();
if (!img) console.log('não encontrou...');
else alert(img);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.