替换多个<;br>;在javascript中,替换为单个<;br>;?
我想将文本中的多个替换多个<;br>;在javascript中,替换为单个<;br>;?,javascript,html,regex,Javascript,Html,Regex,我想将文本中的多个标记替换为单个 我的文字就像 <p>fhgfhgfhgfh</p> <br><br> <p>ghgfhfgh</p> <br><br> <p>fghfghfgh</p> <br><br> <p>fghfghfgh</p> <br><br> <p>fghfgh</p>
标记替换为单个
我的文字就像
<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>
fhgfhgfh
ghgfhfgh
fghfghfgh
fghfghfgh
fghfgh
如何将多个
替换为单个
。试试这个
var str="<p>fhgfhgfhgfh</p><br><br><p>ghgfhfgh</p><br><br><p>";
var n=str.replace(/<br><br>/g,"<br>");
console.log(n);
工作
其中,
/…/
表示正则表达式,(
)
表示
标记,+
表示前面表达式的一个或多个引用,最后,g
用于全局替换。这应该可以做到:
str.replace(/(?:<br>){2,}/g, '<br>')
str.replace(/(?:
){2,}/g,
)
或者,如果它们可以位于不同的线路上:
str.replace(/(?:<br>\s*){2,}/g, '<br>')
str.replace(/(?:
\s*){2,}/g,
'))
str.replace(/(?:<br>\s*){2,}/g, '<br>')