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);