简体   繁体   中英

regex - replace multi line breaks with single in javascript

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.

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');
myText = myText.replace(/\n{2,}/g, '\n');​​​​​​​

看演示

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 .

要替换所有额外的换行符并只留下一种用法:

myText = myText.replace(/\n\n*/g,'\r\n');​​​​​​​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM