Staffer901 Staffer901 - 3 months ago 8x
Javascript Question

Regex Expressions is not capturing the Url inside of a <img tag

Back Ground Information

I am working with SharePoint and Office.Js add in which I am creating. The project owner would like images to be inserted along with text on the fly, however all I am getting the string that I am getting back from my ajax call to SharePoint only brings the URL within in the img tag. This is a string which I am tokenizing by using

for (var x = 0; x < imageToInsert.length; x++) {

seetest = imageToInsert[x].search("<img");

if (seetest >= 0)
cleanedB64 = imageToInsert[x].toString().replace('~~end~~', '');

var teststop = 0;

Working on a project that will need to get the "url" from with in a html img tag that I am receiving from SharePoint. Currently I have an expression which is only catching the < img part of the string but I am not getting the actual URL. The expression I am using is
/img src=([^]*?)
but all I am getting is "img src=" from the string
<img src="/sites/ContentCenter/Graphics/map-al.jpg" alt="map al" style="width&#58;611px;height&#58;262px;" />&#160;<br></p><p><br></p><p>

Desired Result:

I am looking to get the url with in a html img tag that I getting back from sharepoint


I am not sure exactly what you are trying to do, but I am assuming you are just trying to get the that value of a img src tag from a string that you are getting from SharePoint. I good way top approach this is by segmenting the string then getting that tag value. This code has not been tested but it will give you a good idea of what you want to do.

                    var str = selectedContent.toString();
                    var n = str.split("split on Img tags");
                    var urls = [];
                    for (var i = 0; i < n.length; i++) {
                        var post_body = selectedContent;
                        var div = document.createElement('div');
                        div.innerHTML = post_body;
                        var firstImage = div.getElementsByTagName('img')[i]
                        var imgSrc = firstImage ? firstImage.src : "";
                        var rawImgSrc = firstImage ? firstImage.getAttribute("src") : "";

or you can use Jquery, it can be much simpler than using a pure Javascript method this example can guide you in the right direction.

var $myString = $('.classToholdYourstring');

var test = $('img');