admin管理员组

文章数量:1183193

I'm trying to replace any <br /> tags that appear AFTER a </h2> tag. This is what I have so far:

Text = Text.replace(new RegExp("</h2>(\<br \/\>.+)(.+?)", "g"), '</h2>$2');

It doesn't seem to work, can anyone help? (No matches are being found).

Test case:

<h2>Testing</h2><br /><br /><br />Text

To:

<h2>Testing</h2>Text

I'm trying to replace any <br /> tags that appear AFTER a </h2> tag. This is what I have so far:

Text = Text.replace(new RegExp("</h2>(\<br \/\>.+)(.+?)", "g"), '</h2>$2');

It doesn't seem to work, can anyone help? (No matches are being found).

Test case:

<h2>Testing</h2><br /><br /><br />Text

To:

<h2>Testing</h2>Text
Share Improve this question asked Apr 28, 2011 at 22:35 Tom GullenTom Gullen 61.7k87 gold badges291 silver badges468 bronze badges 5
  • 4 Oh lord, please just use a parser. – Matt Ball Commented Apr 28, 2011 at 22:37
  • 1 It's like you're begging me to post a link to this question: stackoverflow.com/questions/1732348/… – Gabe Moothart Commented Apr 28, 2011 at 22:41
  • @Gabe, I don't see how, this is for a WYSIWYG editor I'm writing, it turns \n into <br /> and ##title## into <h2>Title</h2> but now I just want to remove all trailing <br /> after the h2 or it looks bad. – Tom Gullen Commented Apr 28, 2011 at 22:43
  • 1 Use a parser library if available. You would even be better off just writing a quick and simple character-by-character parser. It would actually be less work, more satisfying, easier to understand and less error-prone than regex. And you can add more features easily when you need to. My rule of thumb is regular expression: it's only one or two levels up from tokens. You could use regex to validate a single HTML element or a text node. I would consider that expression-level. But not structured HTML. No doubt someone will come up with a very clever regex which solves your problem. – rohannes Commented Apr 28, 2011 at 22:51
  • @Rohannes, I think regexp is better, because once the form is submitted the data has to be processed server side to produce the same output, so maintaing regexp is easier this way. – Tom Gullen Commented Apr 28, 2011 at 23:32
Add a comment  | 

4 Answers 4

Reset to default 16

This is simpler than you're thinking it out to be:

Text = Text.replace(new RegExp("</h2>(\<br \/\>)*", "g"), "</h2>");

This would do what you are asking:

Text = Text.replace(new RegExp("</h2>(<br />)*", "g"), '</h2>');

If you have jQuery kicking around then you can do this safely without regular expressions:

var $dirty = $('<div>').append('<p>Where is<br>pancakes</p><h2>house?</h2><br><br>');
$dirty.find('h2 ~ br').remove();
var clean = $dirty.html();
// clean is now "<p>Where is<br>pancakes</p><h2>house?</h2>"

This will also insulate against the differences between <br>, <br/>, <br />, <BR>, etc.

You can also make this a little nicer? using the shorthand regex syntax

Text = Text.replace(/<\/h2>(<br\s*\/>)*/g, '</h2>');

本文标签: regexJavascript regexp replace all ltbr gt39sStack Overflow