Я пытаюсь придумать какое-нибудь регулярное выражение, которое я могу использовать для замены последнего пробела неразрывным пробелом (управляющими вдовами) внутри заголовков только внутри блока HTML.
Пока чтоУ меня есть это:
const regex = /(<h.>.+?)\s+((\S|<[^>]+>)*)\n|$/gi
const replaced = text.replace(regex, '$1 $2')
В regex101 похоже, что он работает правильно, но при запуске в JavaScript он добавляет дополнительный  
в конец строки.
Пример блокаHTML может выглядеть следующим образом:
<h2>This is a test heading</h2>
<p>Here is some text</p>
<div>
<h3>Here is a another heading</h3>
<p>Some more paragraph text which shouldn't match</p>
</div>
Что следует заменить на:
<h2>This is a test heading</h2>
<p>Here is some text</p>
<div>
<h3>Here is a another heading</h3>
<p>Some more paragraph text which shouldn't match</p>
</div>
A ссылка на regex101 , показывающая рабочий шаблон.
Ниже приведен фрагмент, показывающий нерабочее поведение в JavaScript:
let text = "<h2>This is a test heading</h2>"
const regex = /(<h.>.+?)\s+((\S|<h.>)*)\n|$/gi
let replaced = text.replace(regex, '$1 $2')
console.log(replaced);
text = `<h2>This is a test heading</h2>
<p>Here is some text</p>
<div>
<h3>Here is a another heading</h3>
<p>Some more paragraph text which shouldn't match</p>
<p>Why is there a non breaking space at the very end?</p>
</div>`
replaced = text.replace(regex, '$1 $2')
console.log(replaced);