简体   繁体   English

替换正则表达式中的 img 元素 src 属性

[英]Replace img elements src attribute in regex

Solved it in case anyone needs it here it is解决了它以防万一有人需要它

var feed = feeds.entries[i].content; var parsedFeed = feed.replace(/src=/gi, "tempsrc="); var tmpHolder = document.createElement('div'); tmpHolder.innerHTML=parsedFeed;

I have a string containing html markup which include <img src='path.jpg'/>我有一个包含 html 标记的字符串,其中包括<img src='path.jpg'/>

I would like to run a regex against the string to replace every src attr to tmpSrc我想对字符串运行正则表达式以将每个 src attr 替换为 tmpSrc

so所以

 <img src='path.jpg'/>

would turn into会变成

 <img tmpSrc='path.jpg'/>

this is in javascript by the way顺便说一句,这是在 javascript

and here is the root issue posted in other places but has not been solved这是在其他地方发布但尚未解决的根本问题

Browser parse HTML for jQuery without loading resources 浏览器解析 HTML 为 jQuery 而不加载资源

How to parse AJAX response without loading resources? 如何在不加载资源的情况下解析 AJAX 响应?

Thanks谢谢

If this is a string you control and not HTML retrieved from a web page, then you can indeed safely use a regex.如果这是您控制的字符串,而不是从 web 页面检索到的 HTML,那么您确实可以安全地使用正则表达式。 To change all occurences of <img src= to <img tmpSrc= , you can use this operation:要将所有出现的<img src=更改为<img tmpSrc= ,可以使用以下操作:

var str = "<img src='path.jpg'/>";   // whatever your source string is
str = str.replace(/<img src=/gi, "<img tempSrc=");

What the other posters have been saying is that regex are not good to use on HTML retrieved from a web page because different browsers return different forms of HTML so it makes it hard to reliably match it. What the other posters have been saying is that regex are not good to use on HTML retrieved from a web page because different browsers return different forms of HTML so it makes it hard to reliably match it. But, if the string you're trying to do a replace on is under your own control and you can know the format of it, then this regex should work fine.但是,如果您尝试替换的字符串在您自己的控制之下并且您可以知道它的格式,那么这个正则表达式应该可以正常工作。

Manipulating HTML with RegExps is error prone.使用 RegExps 操作 HTML 很容易出错。

If you can suffer to include jQuery in your page:如果您可以忍受在您的页面中包含 jQuery :

$('img').each(function() {
    var src = this.src;
    $(this).attr('tmpSrc', src).removeAttr(src);
});
function replace() {
    var images = document.getElementsByTagName('img'),
        srcValue,
        max
        i;

    for (i = 0, max = images.length; i < max; i++) {
       srcValue = images[i].getAttribute('src');
       images[i].setAttribute('tmpSrc', srcValue);
       images[i].removeAttribute('src');
    }
}

as string:作为字符串:

input = " <img src='path.jpg'/>"; 
output = input.replace("src","tmpSrc"); 

using DOM:使用 DOM:

    e = document.getElementById("theimg");  
    e.tmpSrc = e.src;
   e.removeAttribute("src");

Check out this post explaining the problems with using regexes to parse HTML .查看这篇文章,解释使用正则表达式解析 HTML 的问题 It is tricky and probably not worth the effort.这很棘手,可能不值得努力。 I don't think there is any way that is guaranteed to work.我认为没有任何方法可以保证有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM