I am Looking for html string jquery parser ( parse <a> links and <img> images) or for code that will parse all links and images from html string (Html string can be very big).
Example:
input:
sdsds<div>sdd<a href='http://google.com/image1.gif'image1</a> sd</div>
sdsdsdssssssssssssssssssssssssssssssssssss <p> sdsdsdsds </p>
sdsds<div>sdd<img src='http://google.com/image1.gif'image1 alt="car for family"> sd</div>
output links: value+href (if value empty not return such link)
output images:src + alt
Its very important for me to find the most efficient way.
Edit:
the function should looks like that return multi dimensial array.
like: arr[links][href][value] arr[images][src][alt]
function parseLinksAndImages(htmlString){
.........................
........................
return linksAndImagesArrMultiDimensial;
}
(or in other better way if you have)
Thanks
Assuming your string is valid HTML, you could do something like this:
Try it out: http://jsfiddle.net/WsDTL/2/ (updated from original to use
.each()instead of.map()in order to avoid using.split())EDIT:
If you meant that you don’t want to process the
<a>if it doesn’t have anhrefattribute, then change the code to this:EDIT:
For storage as in your comment, you would make
resultsa javascript object, and store the arrays under thelinksandimageskeys.You can do 2 separate loops if you prefer. Not sure which will perform better.
http://jsfiddle.net/WsDTL/3/