[英]Javascript - How to pull to out substring from string when ending location is different
[英]JavaScript substring: how to pull out specific information from a string
我有一个div,背景图片带有“样式”属性。 它拉回以下内容:
background-image: url("<img alt="" src="/mysite/imagegallery/PublishingImages/Gallery/image.png" style="BORDER: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属性。
任何帮助将不胜感激。
您应该在CMS上解决问题。 但是要回答您的问题,您可以这样做。
的HTML
<div id="screwedByCMS" style='background-image: url("<img alt="" src="/mysite/imagegallery/PublishingImages/Gallery/image.png" style="BORDER:0px solid;" />"); background-attachment: scroll; background-repeat: no-repeat; background-position-x: 0%; background-position-y: 0%; background-color: transparent;'></div>
Java脚本
var screwedByCMS = document.getElementById("screwedByCMS"),
imageURL = screwedByCMS.attributes.style.nodeValue.match(/src="(\S+?)"/);
console.log(imageURL[1]);
退货
/mysite/imagegallery/PublishingImages/Gallery/image.png
您可以使用正则表达式
var matches = '<img alt="" src="..."'.match(new RegExp("src=\"([a-zA-Z0-9/.]+)\""));
var srcString = matches[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.