<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="main">
<div class="main2">1</div>
<div class="sub">2</div>
<div class="sub">3</div>
<div class="end">4</div>
<div class="sub">5</div>
<div class="sub">6</div>
<div class="end">7</div>
</div>
<script>
$('.main .main2 ~ .sub').filter(':not(.end ~ .sub)').css('color', 'red');
</script>