替换多个< br>在JavaScript中用单个< br>替换? [英] replace multiple <br> in javascript replace with single <br>?
本文介绍了替换多个< br>在JavaScript中用单个< br>替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在文本中用单个< br>
替换多个< br>
标签。
I want to replace the multiple <br>
tags with single <br>
in a text.
我的文字喜欢,
my text like,
<p>fhgfhgfhgfh</p>
<br><br>
<p>ghgfhfgh</p>
<br><br>
<p>fghfghfgh</p>
<br><br>
<p>fghfghfgh</p>
<br><br>
<p>fghfgh</p>
<br><br>
我如何取代多个< br>
with single < br>
?。
How i replace the multiple <br>
with single <br>
?.
推荐答案
/ p>
Try this
var str="<p>fhgfhgfhgfh</p><br><br><p>ghgfhfgh</p><br><br><p>";
var n=str.replace(/<br><br>/g,"<br>");
console.log(n);
工作 DEMO
编辑:以上为2 br
标签,下面的代码应该可以处理任何数量的 br 标签。
Edit: Above works for 2 br
tags, code below should take care of any number of br tags.
var n = str.replace(/(<br>)+/g, '<br>');
工作 DEMO
其中 /.../
表示正则表达式,(< br>)
表示< br>
标记, +
表示前一个表达式的一次或多次出现,最后 g
用于全局替换。
where /.../
denotes a regular expression, (<br>)
denotes <br>
tag, and +
denotes one or more occurrence of the previous expression and finally g
is for global replacement.
这篇关于替换多个< br>在JavaScript中用单个< br>替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文