title: How can I automatically merge unnecessary html tags
body: <p>How can I merge <code>a</code> tags with the same url together if they are beside each other. For instance, I am dealing with html that looks similar to this:</p>
  
  <pre><code>&lt;div&gt;
      &lt;a href='/url.com'&gt;This is&lt;/a&gt;&lt;a href='/url.com'&gt; the &lt;/a&gt;&lt;a href='/url.com'&gt;same link.&lt;/a&gt;
      This is not linked but might have some &lt;b&gt;bolding&lt;/b&gt; or not.
      &lt;a href='/url.com'&gt;These are&lt;/a&gt;&lt;a href='/url2.com'&gt; two different links.&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Through jQuery, I would like the inside of the div to be:</p>
  
  <pre><code>&lt;div&gt;
      &lt;a href='/url.com'&gt;This is the same link.&lt;/a&gt;
      This is not linked but might have some &lt;b&gt;bolding&lt;/b&gt; or not.
      &lt;a href='/url.com'&gt;These are&lt;/a&gt;&lt;a href='/url2.com'&gt; two different links.&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I can merge tags together by iterating through each pair of <code>a</code> tags to see if they share (1) the same link and (2) the same parent, but then I get output like this:</p>
  
  <pre><code>&lt;div&gt;
      &lt;a href='/url.com'&gt;This is the same link.These are&lt;/a&gt;
      This is not linked but might have some &lt;b&gt;bolding&lt;/b&gt; or not.
      &lt;a href='/url2.com'&gt; two different links.&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I'm not sure how to realize that there's text in the middle.</p>
  
  <p>EDIT: Here's what I've tried</p>
  
  <pre><code>$('a').addClass('linkCheck');
  while ($('.linkCheck').length &gt; 0) {
          first_to_check = $('.linkCheck:first');
          first_to_check.removeClass('linkCheck');
  
          if ($('.linkCheck').length &gt; 0) {
              second_to_check = $('.linkCheck:first');
  
              replaced = false;
              if (first_to_check.attr('href') == second_to_check.attr('href')) {
                  found_first = false;
  
                  old_content_html = $('#divID').html();
                  old_content_text = $('#divID').text();
  
                  first_to_check.parent().children().each(function () {
                      if (found_first == true &amp;&amp; $(this).get(0) == second_to_check.get(0)) {
                          html = second_to_check.html();
                          second_to_check.remove();
                          first_to_check.html(first_to_check.html() + html);
                          found_first = false;
                          replaced = true;
  
                          if ($('#divID').text() != old_content_text) {
                              replaced = false;
                              $('#divID').html(old_content_html);
                          }
  
                          return false;
                      }
  
                      if ($(this).get(0) == first_to_check.get(0)) {
                          found_first = true;
                      }
                      else {
                          found_first = false;
                      }
                  });
  
              }
  
              if (replaced == true) {
                  first_to_check.addClass('linkCheck');
              }
          }
  
      }
  </code></pre>