Q:

Check if an span element contains a "text" and is the last child of a parent element

I am looping around span elements which belongs to "frag" class. I wanted to detect if the frag element im currently in

has parent with 'cond' on its class name
AND
that parent contains the text "if" or "else if" 
AND
the current frag is the last child of the parent qualified at no.1 and no.2

(My system is like a code editor by the way, so i am using a contenteditable div)
Here, i wanted to detect if im on b which is qualified as no.1,2 and 3.
contenteditable div (textarea)
if( (b>3) && b<10)

What i've done dont mind about the loop. I dont know how to construct the condtions.
if( $('.frag').eq(ctr).parent().hasClass('cond') && /(?:if|else\s+if)/g.test$('.frag').eq(ctr).text() && $('.frag').eq(ctr).lastChildOfParent ) 

HTML
<div id="board">
  <div>
  <span class="frag cond">if <span class="openParen bm1">(</span> <span class="frag cond">(<span class="frag">b</span>&gt;3) </span> && <span class="frag">b</span>&lt;10 <span class="closeParen bm1">)</span></span>
  </div>
</div>

UPDATE1: added the span-wrap for the ( and ). This makes ) the last child.
UPDATE2: what i mean about parent is, it can be an immediate parent, a grandParent or grand grand grand parent. So that there will be no issues on text inside the contenteditable div like if( (b>3) && ((c>1)||(b<10)) )
Because when im in the fragment of the last b i wont find an 'if' or 'else if' in my immediate parent (b<10)

A:

You seem to have (1) and (2) ok, so for (3), try:
$elem.nextAll('.frag').length === 0

where $elem is the jQuery object of the element you're testing.
See: http://api.jquery.com/nextAll/
So, overall, you'd want something like:
var $parent = $elem.parent();

if ($parent.hasClass('cond') && /^\s*(if|else\s+if)/.test($parent.text()) && !$elem.nextAll('.frag').length) {
    // do things
}

The only bit I'm unsure of is the regex because I wasn't 100% what you wanted. I went with starting with "if" or "else if".
UPDATE: If I understand your update, then try:
var validAncest = $elem.parents('.cond').filter(function(i){
    return /^\s*(if|else\s+if)/.test($(this).text());
});
// this finds all ancestors with class 'cond' and 
// whose containing text starts with 'if'/'else if'

if (validAncest.length && !$elem.nextAll('.frag').length) {
    // do things
}

UPDATE 2: And if you need to calculate the depth of a descendent within an ancestor, you can use:
function childDepth ($elem, $ancestor) {
    var generations = 0;

    while ($elem[0] && $elem[0] !== $ancestor[0]) {
        generations++;
        $elem = $elem.parent();
    }

    if ($elem[0]) {
        return generations;
    }
}

which will return 1 if $ancestor is the parent of $elem, 2 if it's the grandparent etc.; and it'll return undefined if $ancestor isn't actually an ancestor of $elem.

