Ad

JavaScript Substring: How To Pull Out Specific Information From A String

- 1 answer

I have a div with a "style" attribute for a background image. It pulls back the following:

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;

I would like to strip out the following

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

Once I have this, I can write the JS to replace the style attribute.

Any help would be greatly appreciated.

Ad

Answer

You should fix the problem at your CMS. But to answer your question, you could do this.

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]);

Returns

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

On jsfiddle

Ad
source: stackoverflow.com
Ad