user2028856
user2028856

Reputation: 3183

javascript/jquery remove relative path from url

I'm using jquery to find and retrieve several img src's from the DOM, however the src's are in relative path format like this:

src="../../../../../dynamic/eshop/product_images/thumbnail_cache/366x366/042-1392_13760000.1.jpg"

I need to remove all the trailing slashes and append an absolute url. Is there anyway to achieve this without using regex in javascript or jquery?

Upvotes: 2

Views: 3849

Answers (2)

Spokey
Spokey

Reputation: 10994

Given the URL

var url = '../../../../../dynamic/eshop/product_images/thumbnail_cache/366x366/042-1392_13760000.1.jpg';

Using regex

var fullurl = url.replace(/^.+\.\//,'');

Using index

var inx = url.lastIndexOf('./');
var fullurl2 = url.substring(inx+2, url.length)

Normal replace (if you're sure you only have ../)

url.replace(/\.\.\//g, '');

FIDDLE

Upvotes: 5

maximkou
maximkou

Reputation: 5332

Use split function:

var segments = "/url/to/img".split('/');
alert(segments[segments.length-1]); // your value

Upvotes: 0

Related Questions