const regex = /<h1>([^<]*)<\/h1>\s*\n([\w\W]*?)(([^\n\S]*)<td\s.*?<\/td>(\n))(?=\s*<\/tr>)|(?<=<h1>([^<]*)<\/h1>[\w\W]*?)(([^\n\S]*)<td\s.*?<\/td>(\n))(?=\s*<\/tr>)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('<h1>([^<]*)<\\\/h1>\\s*\\n([\\w\\W]*?)(([^\\n\\S]*)<td\\s.*?<\\\/td>(\\n))(?=\\s*<\\\/tr>)|(?<=<h1>([^<]*)<\\\/h1>[\\w\\W]*?)(([^\\n\\S]*)<td\\s.*?<\\\/td>(\\n))(?=\\s*<\\\/tr>)', 'g')
const str = `<h1>TITLE OF HEAD 1</h1>
<table>
<tbody>
<tr>
<td class="one">ITEM 1, AFTER HEAD 1</td>
</tr>
<tr>
<td class="one">ITEM 2, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td class="one">ITEM 3, AFTER HEAD 1</td>
</tr>
<tr>
<td class="one">ITEM 4, AFTER HEAD 1</td>
</tr>
<tr>
<td class="one">ITEM 5, AFTER HEAD 1</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 2</h1>
<table>
<tbody>
<tr>
<td class="one">ITEM 6, AFTER HEAD 2</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 3</h1>
<table>
<tbody>
<tr>
<td class="one">ITEM 7, AFTER HEAD 3</td>
</tr>
<tr>
<td class="one">ITEM 8, AFTER HEAD 3</td>
</tr>
<tr>
<td class="one">ITEM 9, AFTER HEAD 3</td>
</tr>
<tr>
<td class="one">ITEM 10, AFTER HEAD 3</td>
</tr>
</tbody>
</table>
<h1>TITLE OF HEAD 4</h1>
<table>
<tbody>
<tr>
<td class="one">ITEM 11, AFTER HEAD 4</td>
</tr>
<tr>
<td class="one">ITEM 12, AFTER HEAD 4</td>
</tr>
</tbody>
</table>
`;
const subst = `$2$3$4$7$8<td class="two">$1$6</td>$5$9`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions