使用jquery,从更大的兄弟组中提取每组相邻兄弟,匹配选择器

Using jquery, extract each group of adjacent siblings, matching selector, from a larger group of siblings

我有以下标记:

<div id="parent">
    <div class="special child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="special child"></div>
    <div class="special child"></div>
    <div class="special child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

我需要做的是提取 div.special 的 2 个子集合(对于 2 组相邻的兄弟姐妹),然后用另一个父级包装每个组,最后是:

<div id="parent">
    <div class="special_parent">
        <div class="special child"></div>
    </div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="special_parent">
        <div class="special child"></div>
        <div class="special child"></div>
        <div class="special child"></div>
    </div>
    <div class="child"></div>
    <div class="child"></div>
</div>

我可以先遍历同级元素并完成一些繁重的工作,但我确信应该有更优雅的方法。

使用each() and nextUntil()

$('.special').each(function() {
  if ($(this).parent().hasClass('special_parent')) return
  $(this)
    .nextUntil(':not(.special)')
    .add(this)
    .wrapAll('<div class="special_parent">')
})
.special_parent {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="parent">
  <div class="special child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="special child">5</div>
  <div class="special child">6</div>
  <div class="special child">7</div>
  <div class="child">8</div>
  <div class="child">9</div>
  <div class="special child">10</div>
  <div class="special child">11</div>
</div>