Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换多个<;br>;在javascript中,替换为单个<;br>;?_Javascript_Html_Regex - Fatal编程技术网

替换多个<;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>')