简体   繁体   English

javascript / jquery从url中删除相对路径

[英]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: 我正在使用jquery从DOM中查找和检索几个img src,但是src是相对路径格式,如下所示:

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. 我需要删除所有尾部斜杠并附加一个绝对URL。 Is there anyway to achieve this without using regex in javascript or jquery? 无论如何,如果不在javascript或jquery中使用正则表达式来实现这一点?

Given the URL 鉴于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 小提琴

Use split function: 使用拆分功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM