Nir Tzezana
Nir Tzezana

Reputation: 2342

replace two br with single one jquery

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.

Upvotes: 1

Views: 869

Answers (4)

moonwave99
moonwave99

Reputation: 22820

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

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

});

If your html is not coming from the DOM:

$(thishtml).find('br').map( ... )

Upvotes: 7

Yjae Dalina
Yjae Dalina

Reputation: 106

try this:

.replace(/(\<br[\s]*\/\>){2,}/g, '<br/>')

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148744

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

Upvotes: 1

CrayonViolent
CrayonViolent

Reputation: 32537

Try

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

Upvotes: 3

Related Questions