去掉 <br /><br /> 标签,并替换为 </p><p> 标签

Strip out <br /><br /> tags and replace with </p><p> tags

本文关键字:标签 br 替换 去掉      更新时间:2023-09-26

我的同事在 asp.net 为我做了一些后端开发,我做了带有文本区域的CMS输入页面。问题是他们在生成页面时输出带有<br /><br />标签的段落。空间很大,我更喜欢<p></p>这样我就可以控制间距等。我只想更改和替换 ID #FeaturedProjectNewsWrapper中的<br />标签。

我谷歌了一个小时左右,只找到了这个 Javascript 方法,我尝试将<br>更改为 <br /> 但它不起作用,当我在页面上放一个时它不起作用......

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{$('body').html($('body').html().replace(/<br>'*/g,"</p><p>"));});
</script>

我还发现了一些PHP的东西,但这没有任何用处。我不确定如何真正使用ASP,所以一点Javascript解决方案会很好。

这是我的代码...

<div id="FeaturedProjectNewsWrapper">
<p class="DateStyle">20/05/2011</p>
<h2 class="ProjectNews">Employee of the Month</h2>
<p>LTM operate an Employee of the month scheme.  This was set up in 2009 to recognise and reward the hard work and dedication of individual employees across all LTM divisions.  Each month nominations are forwarded to Steven Laing who makes the final decision.  The employee is then notified and awarded £100 cash reward for their commitment and contribution to LTM’s ongoing success.<br /><br />The Employee of the Month recognition is not a substitute for our annual Employee of the Year award, the day-to-day positive recognition or reasonable pay and benefits the company has to offer.  It is really just another way of giving a special thank you to our top employee’s for their outstanding contribution in that month.<br /><br />Employee of the Month - April 2011: Zac Manning<br /><br />Congratulations go to Zac for receiving his Postgraduate Diploma in Surveying with distinction from the College of Estate Management, and also for his efforts at achieving the cHeRries Awards nominee stage for LTM Training Academy!</p>
<br style="clear:both;" />
</div>

这是网址,如果它有任何帮助...

http://www.traditionalmasonry.co.uk/News/CompanyNewsArticle.aspx

非常感谢,我希望我提供了足够的信息。

请不要在客户端使用 javascript 这样做,如果你尊重其他将要追随你的人的工作。我知道asp看起来很糟糕,但最好清理那里的代码。对项目进行全局搜索,并找到具有ID为"FeaturedProjectNewsWrapper"的项目的页面。ASP 页通常类似于意大利面条,但至少插入分隔符而不是段落的代码很有可能在同一个文件中。

您遇到的问题是,对于<br/>标签,段落从两者之间去标记,而段落周围是<p>标签。 请考虑以下等效项:

<p>first</p>
<p>second</p>
<p>third</p>
first<br/>
second<br/>
third<br/>

如果将<br/>替换为</p><p>则省略开头<p>和尾随</p>。 为了进行此转换,您必须能够插入开始<p>和结束</p>标签,否则它将无法正常工作。 或者,您可以使用 CSS 控制<br/>间距。

呵。

尝试使用这个正则表达式:/<br[^>]*>([^<]+)(<br>)/,"<p>$1</p>"