Replace img attribute of src elements in regex

Decided him, if someone needs him, he

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

I have a line containing html markup that includes <img src='path.jpg'/>

I would like to run a regex for a string to replace each src attr with tmpSrc

So

 <img src='path.jpg'/>

will turn into

 <img tmpSrc='path.jpg'/>

this is in javascript by the way

and here is the root problem, posted elsewhere but not resolved

Browser parses HTML for jQuery without loading resources

How to parse an AJAX response without loading resources?

thank

0
source share
5 answers

, , HTML, -, . <img src= <img tmpSrc=, :

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

, , , HTML, -, HTML, . , , , , .

+2

HTML RegExps .

jQuery :

$('img').each(function() {
    var src = this.src;
    $(this).attr('tmpSrc', src).removeAttr(src);
});
+1
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');
    }
}
0
source

like string:

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

using the DOM:

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

A message opens explaining the problems with using regular expressions to parse HTML . This is complicated and probably not worth the effort. I don’t think there is any way that is guaranteed to work.

0
source

All Articles