1

I'd like to remove unnecessary strings: <br>, <br/> and <br /> from a string, that might contain many of them in row. Example:

This is a string with <br><br/> many <br/><br> newlines. <br><br><br /><br> But it's ok <br> to have one!

I would like it to become this:

This is a string with <br> many <br> newlines. <br> But it's ok <br> to have one!

Ziarno
  • 7,366
  • 5
  • 34
  • 40

2 Answers2

3

You can do

str = str.replace(/<\/?br\s*\/?>\s*(<\/?br\s*\/?>)+/ig, '<br>');
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2
var str = "This is a string with <br><br/> many <br/><br> newlines. <br><br><br /><br> But it's ok <br> to have one!";
var cleaned = str.replace(/(<br\s?\/?>\s?)+/g,"<br/> ");

And now the bad news, it is really a bad idea to match HTML tags with a regular expression. See this question: RegEx match open tags except XHTML self-contained tags

An alternative would be to set it to a element, loop through the nodes and remove the tags.

Quick and rough code, did not do a lot of testing on this

function removeDupeBrs (elem) {
    var childrenNodes = elem.childNodes;
    for (var i = childrenNodes.length-1; i>=0 ; i--) {
        var cn = childrenNodes[i];
        if (cn.nodeType===1 && cn.tagName==="BR") {
            var next = childrenNodes[i-1];
            if (next && next.nodeType===1 && next.tagName==="BR") {
                elem.removeChild(cn);                
            }
        }
    }    
}

var str = "This is a string with <br><br/> many <br/><br> newlines. <br><br><br /><br> But it's ok <br> to have one!";
var div = document.createElement("div");
div.innerHTML = str;
removeDupeBrs(div);
console.log(div.innerHTML);
Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236