1

Trying to replace two <br /> with a single one.
Can't get it work. been trying for a couple of hours.

My code: thishtml = thishtml.replace(/(?:<br \/\>\s*){2,}/g, '<br>')

Not working.
Help will be great.

Thanks ahead.

Nir Tzezana
  • 2,275
  • 3
  • 33
  • 56
  • [This question](http://stackoverflow.com/questions/19861184/remove-sequential-br-in-div-filled-by-cms-users-via-rich-text-editors/19862178) may help. – Jason P Nov 14 '13 at 13:46
  • possible duplicate of [Replace multiple
    's with only one
    ](http://stackoverflow.com/questions/17061931/replace-multiple-brs-with-only-one-br)
    – Qantas 94 Heavy Nov 14 '13 at 13:47

4 Answers4

7
$('br').map(function(){

  ($next = $(this).next()).is('br') && $next.remove();

});

If your html is not coming from the DOM:

$(thishtml).find('br').map( ... )
moonwave99
  • 21,957
  • 3
  • 43
  • 64
3

Try

thishtml = thishtml.replace(/(?:<br[^>]*>\s*){2,}/g, '<br>')

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
1

try this :

.replace(/(?:<\s*br\s*\/?\>\s*){2,}/ig, '<br/>');

example :

var a='sdf sd\
sdf\
sdf\
<br    />\
    <br/>\
  dfbd'

result :

sdf sdsdfsdf<br/>dfbd

http://jsbin.com/OfEbaCA/6/edit

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

try this:

.replace(/(\<br[\s]*\/\>){2,}/g, '<br/>')
Yjae Dalina
  • 106
  • 5