JavaScript子字符串:如何从字符串中提取特定信息

时间:2013-06-12 14:44:30

标签: javascript substring

我有一个带有“样式”属性的div用于背景图像。 它取消了以下内容:

background-image: url("<img alt="" src="/mysite/imagegallery/PublishingImages/Gallery/image.png" style="BORDER&#58;0px solid;" />"); background-attachment: scroll; background-repeat: no-repeat; background-position-x: 0%; background-position-y: 0%; background-color: transparent;

我想删除以下

/mysite/imagegallery/Images/Gallery/image.png

有了这个,我可以编写JS来替换style属性。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您应该在CMS上解决问题。但要回答你的问题,你可以这样做。

HTML

<div id="screwedByCMS" style='background-image: url("<img alt="" src="/mysite/imagegallery/PublishingImages/Gallery/image.png" style="BORDER&#58;0px solid;" />"); background-attachment: scroll; background-repeat: no-repeat; background-position-x: 0%; background-position-y: 0%; background-color: transparent;'></div>

的Javascript

var screwedByCMS = document.getElementById("screwedByCMS"),
    imageURL = screwedByCMS.attributes.style.nodeValue.match(/src="(\S+?)"/);

console.log(imageURL[1]);

返回

/mysite/imagegallery/PublishingImages/Gallery/image.png 

jsfiddle

答案 1 :(得分:-2)

你可以使用正则表达式

var matches = '<img alt="" src="..."'.match(new RegExp("src=\"([a-zA-Z0-9/.]+)\""));
var srcString = matches[1];