Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  18] [ 6]  / answers: 1 / hits: 39408  / 12 Years ago, sat, june 9, 2012, 12:00:00

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.


More From » regex

 Answers
114

Try this:



text.replace(/ns*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(/ns*ns*n/g, 'nn');


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



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

[#85015] Friday, June 8, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
magaly

Total Points: 524
Total Questions: 96
Total Answers: 89

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
magaly questions
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Nov 8, 20, 00:00, 4 Years ago
Mon, Oct 21, 19, 00:00, 5 Years ago
Mon, Jul 15, 19, 00:00, 5 Years ago
;