用javascript替换多行中断。

时间:2022-03-06 12:13:27

this is some kind of variable content in javascript:

这是javascript中的某种可变内容:

    <meta charset="utf-8">

    <title>Some Meep meta, awesome</title>




    <-- some comment here -->
    <meta name="someMeta, yay" content="meep">

</head>

I want to reduce the multi line breaks (unknown number) to a single line break while the rest of the formatting is still maintained. This should be done in javascript with a regex.

我希望在保持其余格式的同时,将多行中断(未知数字)减少为单行中断。这应该用一个regex在javascript中完成。

I have problems with the tabulator or to keep the format.

我在制表符或保持格式上有问题。

3 个解决方案

#1


45  

Try this:

试试这个:

text.replace(/\n\s*\n/g, '\n');

This basically looks for two line breaks with only whitespace in between. And then it replaces those by a single line break. Due to the global flag g, this is repeated for every possible match.

这基本上是寻找两个换行符之间只有空格。然后它用一个换行代替。由于全局标记g,这在每一个可能的匹配中都是重复的。

edit:

is it possibile to leave a double line break instead of a single

用双线而不是单线可以吗

Sure, simplest way would be to just look for three line breaks and replace them by two:

当然,最简单的办法就是找三个换行符,用两个换行符:

text.replace(/\n\s*\n\s*\n/g, '\n\n');

If you want to maintain the whitespace on one of the lines (for whatever reason), you could also do it like this:

如果您希望在其中一行上维护空格(无论出于什么原因),您也可以这样做:

text.replace(/(\n\s*?\n)\s*\n/, '$1');

#2


8  

myText = myText.replace(/\n{2,}/g, '\n');​​​​​​​

See demo

#3


2  

Given the following (remember to encode HTML entities such as <, > and (among others, obviously) &):

给定以下内容(请记住对HTML实体进行编码,如<、>和(在其他情况下,显然是)&):

<pre>
&lt;head&gt;

    &lt;meta charset="utf-8"&gt;

    &lt;title&gt;Some Meep meta, awesome&lt;/title&gt;




    &lt;-- some comment here -->
    &lt;meta name="someMeta, yay" content="meep"&gt;

&lt;/head&gt;
</pre>
<pre>
</pre>​

The following JavaScript works:

以下JavaScript工作:

var nHTML = document.getElementsByTagName('pre')[0].textContent.replace(/[\r\n]{2,}/g,'\r\n');
document.getElementsByTagName('pre')[1].appendChild(document.createTextNode(nHTML));​

JS Fiddle demo.

JS小提琴演示。

#1


45  

Try this:

试试这个:

text.replace(/\n\s*\n/g, '\n');

This basically looks for two line breaks with only whitespace in between. And then it replaces those by a single line break. Due to the global flag g, this is repeated for every possible match.

这基本上是寻找两个换行符之间只有空格。然后它用一个换行代替。由于全局标记g,这在每一个可能的匹配中都是重复的。

edit:

is it possibile to leave a double line break instead of a single

用双线而不是单线可以吗

Sure, simplest way would be to just look for three line breaks and replace them by two:

当然,最简单的办法就是找三个换行符,用两个换行符:

text.replace(/\n\s*\n\s*\n/g, '\n\n');

If you want to maintain the whitespace on one of the lines (for whatever reason), you could also do it like this:

如果您希望在其中一行上维护空格(无论出于什么原因),您也可以这样做:

text.replace(/(\n\s*?\n)\s*\n/, '$1');

#2


8  

myText = myText.replace(/\n{2,}/g, '\n');​​​​​​​

See demo

#3


2  

Given the following (remember to encode HTML entities such as <, > and (among others, obviously) &):

给定以下内容(请记住对HTML实体进行编码,如<、>和(在其他情况下,显然是)&):

<pre>
&lt;head&gt;

    &lt;meta charset="utf-8"&gt;

    &lt;title&gt;Some Meep meta, awesome&lt;/title&gt;




    &lt;-- some comment here -->
    &lt;meta name="someMeta, yay" content="meep"&gt;

&lt;/head&gt;
</pre>
<pre>
</pre>​

The following JavaScript works:

以下JavaScript工作:

var nHTML = document.getElementsByTagName('pre')[0].textContent.replace(/[\r\n]{2,}/g,'\r\n');
document.getElementsByTagName('pre')[1].appendChild(document.createTextNode(nHTML));​

JS Fiddle demo.

JS小提琴演示。