function replaceParentChild(parentSelector, childSelector, replacementTag) {
  $(parentSelector).each(function () {
    var $p = $(this);
    $p.children(childSelector).each(function () {
      $p.replaceWith('<' + replacementTag + '>' + $(this).html() + '</' + replacementTag + '>');
    });
  });
}

$(document).ready(function () {
  replaceParentChild('U', 'B', 'H2');
  replaceParentChild('B', 'U', 'H2');
  
  $('H2+BR').each(function () {
    $(this).remove();
  });
  
  $('P').each(function () {
    var
      parent = $(this),
      children = parent.children();
    
    if (children.length == 1 && children.eq(0).is('H2')) {
      parent.replaceWith(children.eq(0));
    }
  });
  
});
