1

Does anyone know how could I get the image URL from a string? I may get 2 URL's or more than two delimited by a comma and some of them won't not be an image.

 var sample_str = "http://domain.com/test.html,http://another.domain.com/images/1234.jpg";

Basically what I want to do is get only the image URL and omit the rest.

 http://another.domain.com/images/1234.jpg
carol1287
  • 375
  • 3
  • 17

1 Answers1

1

Use split and run through the values. Along the lines of:

var s = "http://domain.com/test.html,http://another.domain.com/images/1234.jpg";
var ss = s.split(",");
for (var i=0; i<ss.length; i++) {
//for (var i in ss) {
    document.write(ss[i]);
    document.write("<br/>");
}
Anthony Horne
  • 2,324
  • 2
  • 26
  • 49
  • Shout if you cannot work from here or need a more complete answer. – Anthony Horne Jan 28 '15 at 09:54
  • Don't use `for..in` for iterating arrays. – Xotic750 Jan 28 '15 at 09:55
  • @Xotic750 I get the impression this will be a very small value list, so didn't think it would matter. – Anthony Horne Jan 28 '15 at 09:56
  • @Xotic750 Why we shouldn't use `for..in` for iterating over arrays? – frogatto Jan 28 '15 at 09:58
  • 1
    `for..in` is for enumeration. http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea Also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in `Note: for..in should not be used to iterate over an Array where index order is important.` and if you are then use at your own peril. – Xotic750 Jan 28 '15 at 10:02
  • For the sake of semantics I have updated it, but since no actual changes are being made to the array I see no difference. Thanks to @Xotic750 for highlighting some best practices. Either way, hopefully it helps the question poster. – Anthony Horne Jan 28 '15 at 10:09
  • [`document.write`](https://developer.mozilla.org/en-US/docs/Web/API/document.write) has its place occasionally as it has its own set of problems, when possible it is best to avoid it and use `document.createElement` and friends. http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Xotic750 Jan 28 '15 at 10:20
  • Thanks so much Anthony, but how do I know which one is an image link? – carol1287 Jan 28 '15 at 10:20
  • thanks a lot, I will add then something like this inside the loop: var extension = ss[i].substr((file.lastIndexOf('.') + 1)); switch (extension) { case 'jpg': alert('image url'); case 'png': alert('image url'); case 'gif': alert('image url'); break; // the alert ended with pdf instead of gif. default: alert('something else'); } – carol1287 Jan 28 '15 at 10:34
  • 1
    If I were to choose between using [`slice`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice), [`substr`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) or [`substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) then I would opt for `slice`. The ES6 [`endsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) does not have wide enough support yet. You could also `split('.')` and `pop`. – Xotic750 Jan 28 '15 at 10:51