DOM manipulation

I need help with the following using javascript or jQuery.

I need to perform the following operations on a string like the below. There are multiple like this on the page and the main identifier is “ms-gb” class on the td tag.

<tbody id=“titl4909-1_” groupString=“%3B%23BOE%204%2E0%20Shop%20Cost%20Study%3B%23”><tr id=“group0”><td colspan=“100” nowrap class=“ms-gb”><a href=“javascript:” onclick=“javascript:ExpCollGroup(‘4909-1_’, ‘img_4909-1_’,event, false);return false;”><img src=“/layouts/images/minus.gif" border=“0” alt=“collapse” id="img_4909-1” /> L1</a> : My Test File Name<span style=“font-weight: lighter”> ‎(3)</span></td></tr></tbody>

1.If the text in between closing anchor tag “</a>” and beginning span tag “<span” is just 3 characters long (that is " : "), I would like to remove the whole parent tbody tag.

Else, I would like to do the following

  1. I would like to replace " L1" between image close tag “/>” and anchor close tag “</a>”. The text " L1" is not static and can be different.

  2. I would like to remove " : " which appears after closing anchor tag “</a>”

  3. I would like to remove the entire span tag.

Something like this will get you part of the way there:


// Get any "blank" rows
var $blankNodes = jQuery('table tr').find('*').contents().filter(function(){
    return this.nodeType === 3 && (/^\\s:\\s$/.exec(this.nodeValue) !== null);
});

// Remove the tbody if the file name isn't present
if ($blankNodes.length) {
   $blankNodes.parent().remove();
}

// We know the remaining nodes contain valid content now
// Remove all spans
jQuery('table tr span').remove();

The other part you described about replacing content within the text nodes is a little trickier, you could use something similar to the method used to detect which nodes contained only ’ : ', but adjust the regular expressions to find the patterns you know about.