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,096
  • 3
  • 28
  • 53
  • [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

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

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

});

If your html is not coming from the DOM:

$(thishtml).find('br').map( ... )
moonwave99
  • 20,750
  • 2
  • 41
  • 64
2

Try

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

Crayon Violent
  • 31,449
  • 5
  • 53
  • 77
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
  • 138,711
  • 129
  • 435
  • 755
0

try this:

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