简体   繁体   English

正则表达式从属性中查找图像src / path

[英]Regex to find image src/path from attribute

if my html is 如果我的HTML是

<div style="background-image: url(http://domain.com/randompath/random4509324041123213.jpg);"></div>

And I get style attribute like.. 然后我得到样式属性。

var img_src = $('div').css('background-image');

So output is 所以输出是

url("http://domain.com/randompath/random4509324041123213.jpg")

Any regex or some basic thing to find image src/path 100% ? 任何正则表达式或一些基本的东西来找到图像src / path 100%? like 喜欢

http://domain.com/randompath/random4509324041123213.jpg

100% I mean cross browser, It should work 100% 100%我的意思是跨浏览器,应该可以工作100%

I not sure about output from var img_src = $('div').css('background-image'); 我不确定来自var img_src = $('div').css('background-image'); has pattern like this in all browsers , If "yes" so I can do .replace() function 在所有浏览器中都有这样的模式,如果为“是”,则可以执行.replace()函数

Playground : http://jsbin.com/apalat/1/edit 游乐场: http : //jsbin.com/apalat/1/edit

Try this one: 试试这个:

$("code").html(img_src.slice(5, -2));

CHECKOUT FIDDLE 结帐场地

Update: 更新:

For 'chrome' because chrome doesn't add '"' quotes: For 'chrome'因为chrome不添加'"' quotes:

var img_src = $('div').css('background-image');

$("code").html(img_src.slice(5, -2));

if (navigator.userAgent.toLowerCase().indexOf('chrome') >= 0) {
   $("code").html(img_src.slice(4, -1));
}

UPDATED FIDDLE 更新场

Don't need regex, just cut the string... 不需要正则表达式,只需剪断字符串...

http://jsfiddle.net/BHcsQ/2/ http://jsfiddle.net/BHcsQ/2/

var s = 'url("http://domain.com/randompath/random4509324041123213.jpg")';
var s_sub = s.substring(5, s.length-2);

alert(s_sub);

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

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