Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 946645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:57:09+00:00 2026-05-15T22:57:09+00:00

I am Looking for html string jquery parser ( parse <a> links and <img>

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:57:10+00:00Added an answer on May 15, 2026 at 10:57 pm

    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() )

    var string = "sdsds<div>sdd<a href='http://google.com/image1.gif'>image1</a>  sd</div>sdsdsdssssssssssssssssssssssssssssssssssss <p> <a href='some/href'></a> sdsdsdsds  </p>sdsds<div>sdd<img src='http://google.com/image1.gif' alt='car for family' />  sd</div>";
    
    var $container = $('<div/>').html(string);
    
    var result = [];
    
    $container.find('a,img').each(function() {
        if(this.tagName.toUpperCase() == 'A') {
            if($.trim( this.innerHTML ) != '') {
                result.push([this.tagName,this.innerHTML,this.href]);
            }
        } else {
            result.push([this.tagName,this.src,this.alt]);
        }
    });
    
    alert(result);​
    

    EDIT:

    If you meant that you don’t want to process the <a> if it doesn’t have an href attribute, then change the code to this:

    $container.find('a[href],img').each(function() {
        if(this.tagName.toUpperCase() == 'A') {
            result.push([this.tagName,this.innerHTML,this.href]);
        } else {
            result.push([this.tagName,this.src,this.alt]);
        }
    });
    

    EDIT:

    For storage as in your comment, you would make results a javascript object, and store the arrays under the links and images keys.

    var string = "sdsds<div>sdd<a href='http://google.com/image1.gif'>image1</a>  sd</div>sdsdsdssssssssssssssssssssssssssssssssssss <p> <a href='some/href'></a> sdsdsdsds  </p>sdsds<div>sdd<img src='http://google.com/image1.gif' alt='car for family' />  sd</div>";
    
    var $container = $('<div/>').html(string);
    
       // store results in a javascript object
    var result = {
         links:[],
         images:[]
    }; 
    
    $container.find('a[href],img').each(function() {
        if(this.tagName.toUpperCase() == 'A') {
            result.links.push([this.tagName,this.innerHTML,this.href]);
        } else {
            result.images.push([this.tagName,this.src,this.alt]);
        }
    });
    
    alert(result.links);
    alert(result.images);
    

    You can do 2 separate loops if you prefer. Not sure which will perform better.​

    http://jsfiddle.net/WsDTL/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a way to get a HTML element from a string that
I know I can put escaped HTML tags in string resources. However, looking at
Greetings, I'm looking for a way to encode a string into HTML that uses
I am currently looking into spliting a very long string that could contain HTML
HI, I want to find all input elements in a HTML string that has
I see lots of php libraries that can parse html. A nice example is
Using an .net MVC2 app is generating a string of HTML code that be
I am looking for a jQuery function to parse a section of HTML and
I'm looking for a HTML editor that kinda supports templated editing or live snippets
Trying to customize Symfony2 form to produce html code looking like the following example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.