JSFiddle - React, Tailwind, and code Playground

HTML

<h1>
Test
</h1>

<p>
  This text is a test.
  <br>
  
  
  
  
  
  <br>
  It should work with 3 and random spaces in between
  <br>    <br>
  <br>
  It should also work with one
  <br>
  Another test
</p>

JavaScript

$('br').each(function () {
  const {nodeName} = this;

  let node = this;
  
  while (node = node.previousSibling) {
    if (node.nodeType !== Node.TEXT_NODE || node.nodeValue.trim() !== '') {
      break;
    };
  }
    
  if (node && node !== this && node.nodeName === nodeName) {
    $(node).remove();
  }
});