codelove
codelove

Reputation: 2016

Remove extra br tags with JS

I have a post which contains extra line breaks and want to limit the post to only show one linebreak. I thought this css might work..

br+br{display:none}

but since the text is not wrapped in its own element all the line breaks in the post are siblings and this doesn't work... now I am trying to solve this with JS...

content.replace(/<br><br>/g,'<br>')

Why is this only replacing the first set of linebreaks that are next to each other? (I need to run it multiple times to get the effect I want of all uneccesary line breaks being removed) and what should I do instead?

Upvotes: 1

Views: 1921

Answers (1)

Blender
Blender

Reputation: 298392

If your regex represents your HTML exactly, this should work:

content.replace(/(<br>)+/g,'<br>')

Although your CSS should've worked: http://jsfiddle.net/UvVbE/

Upvotes: 3

Related Questions