正则表达式未捕获<img tag

时间:2016-07-08 20:11:19

标签: javascript regex string

="" 背景信息中的网址

我正在使用SharePoint和Office.Js添加我正在创建的。项目所有者希望图像与动态文本一起插入,但是我得到的字符串我从ajax调用返回到SharePoint只会将URL带入img标记内。这是一个字符串,我使用

进行标记
            for (var x = 0; x < imageToInsert.length; x++) {

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

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

        var teststop = 0;
    }
    InsertPlainText(s

问题 处理需要从我从SharePoint接收的html img标记中获取“url”的项目。目前我有一个表达式,它只捕获&lt; img部分字符串,但我没有得到实际的URL。我使用的表达式是/img src=([^]*?),但我得到的是来自字符串<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>的“img src =”。

期望的结果:

我希望通过html img标记获取我从sharepoint返回的网址

4 个答案:

答案 0 :(得分:0)

试试http://www.regexr.com。它使构建正则表达式变得非常容易。如果您只是在寻找/sites/ContentCenter/Graphics/map-al.jpg,那么请尝试这样的事情

img src="([^"]*) 

imgsrc="(之后,它会将所有内容分组,直到下一个收尾报价。

答案 1 :(得分:0)

首先,/img src=([^]*?)没有给你任何东西的原因是因为你在img src=之后发表任何言论,更多次,懒惰,没有任何东西。所以,0场比赛是最懒的,这就是答案!

其次,[^]是&#34;而不是nothing&#34;。只需将.用于&#34;任何&#34;

  

仅供参考:所有这些公式都是指向regex101的链接

img src="(.*?)"可能是最容易理解的:在img src="抓住任何内容之后,懒洋洋地,直到你点击"。但它的效率低于(找到解决方案的98个步骤)而不是

img src="([^"]*) 17个步骤。 (捕获所有内容,直到你点击"然后停止)(这是Justin建议的,并且是最佳解决方案)

如果您希望两条最外层"之间的所有内容都行,请回复Justin的帖子,

img src="(.*)"将匹配/sites/ContentCenter/Graphics/map-al.jpg" alt="map al" style="width&#58;611px;height&#58;262px;这会捕获所有内容,直到字符串中的最后一个"

您可能还想引用How to access capture groups in Javascript,因为这些情况下的匹配将位于捕获组1中。

答案 2 :(得分:0)

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") : "";
                        urls.push(rawImgSrc);
                    }

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

  $(test).each(function(d)
{
  console.log($(this).attr('src'));
});

答案 3 :(得分:0)

要从字符串中获取URL,请尝试以下方法:

var text = '<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>'
var reg = /<img src="(.+?)"/;
var url = text.match(reg)[1];

此正则表达式查找<img src="与下一个最接近"之间的任何内容,以及括号内的任何内容。

匹配调用返回一个数组,其中第一个元素是匹配项,每个后续元素是您定义的任何组。然后,您可以使用数组表示法访问这些组。

相关问题