regex - replace multi line breaks with single in j

2020-02-08 12:34发布

问题:

this is some kind of variable content in 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.

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

回答1:

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.

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:

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

See demo



回答3:

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

<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:

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.