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 Answers
Reset to default 16This 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
版权声明:本文标题:regex - Javascript regexp replace all <br >'s - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738299703a2073546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
\n
into<br />
and##title##
into<h2>Title</h2>
but now I just want to remove all trailing<br />
after theh2
or it looks bad. – Tom Gullen Commented Apr 28, 2011 at 22:43