I am currently pulling an XML feed into a webapp through ajax. Obviously i like using jquery to parse it, like so:
$(source).find("item");
The problem with this is that everything is converted into nodes, including -tags. Considering this is some sort of RSS feed, and the feed contains complete articles (including image galleries), there are many img-tags. To prevent this, i would like to try and convert the img-tags to something like this:
before:
<img src="path_to_img.jpg" width="450" height="199" alt="alt description" title="image title" class="image_classes" />
after:
<image>
<src>path_to_img.jpg</src>
<alt>alt description</alt>
<title>image title</title>
<class>image_classes</class>
</image>
If anyone has better suggestions than using regexes, those are of course welcome too. But because it all has to be treated like text, I fear there are few, considering images start preloading when they are added to the DOM.
Well, it’s not going to be very easy unless you can guarantee all those attributes will always be present, and always in quoted strings etc etc.
If your source HTML is not as uniform, then you might have to use
.replace(regex, func)instead of.replace(regex, string).As you have said the alt attribute may not be present, then you will need to use regex with a function, like this: