regex with optional block of text
<p>I'm using regex to parse structured text as below, with caret symbol marking what I'm trying to match:</p>

<pre><code>block 1
^^^^^^^
    subblock 1.1
        attrib a=a1
    subblock 1.2
        attrib b=b1
                 ^^
block 2
    subblock 2.1
        attrib a=a2
block 3
^^^^^^^
    subblock 3.1
        attrib a=a3
    subblock 3.2
        attrib b=b3
                 ^^
</code></pre>

<p>A subblock may or may not appear inside a block, e.g.: <em>subblock 2.2</em>.</p>

<p>The expected match is <em>[(block1,b1), (block3,b3)]</em>.</p>

<pre><code>/(capture block#)[\s\S]*?attrib\sb=(capture b#)/gm
</code></pre>

<p>But this ends up matching <em>[(block1, b1), (block2, b3)]</em>.</p>

<p>Where am I doing the regex wrong?</p>