wawanopoulos
wawanopoulos

Reputation: 9794

Extract url from javascript String

I would like to extract the following String :

http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg

from this String :

https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg

And before, extract, i would like to check if the global String contains more than one time "http" to be sure to extract the jpg only when needed.

How can i do that ?

Upvotes: 0

Views: 234

Answers (4)

radia
radia

Reputation: 1476

You can use regex :

str.match(/(http?:\/\/.*\.(?:png|jpg))/i)

FIDDLE

Upvotes: 0

Pratik Parekh
Pratik Parekh

Reputation: 447

use split()

var original = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg";
original = original.split('-/');
alert($(original)[original.length-1]);

your require URL shows in alert dialog

Upvotes: 0

chris97ong
chris97ong

Reputation: 7060

Extract the data like this:

var myStr = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var splittedStr = myStr.split("-");
var extractedStr = splittedStr[3].slice(1);

To find out how many "http" is present in the string:

var count = (myStr.match(/http/g)).length;

Upvotes: 1

hungneox
hungneox

Reputation: 9829

Hopes it helps

var source = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var temp = source.replace("https","http").split("http");
var result = 'http'+temp[2];

Upvotes: 0

Related Questions