Wonka Wonka - 3 months ago 10
Javascript Question

How to clean up multiple spaces/linebreaks?

I have a variable

summary
like this:

T he
Fox


Jumped



Over


There are 3 parts to clean
summary
:


  • 2+ Consecutive spaces should become 1 space, see below
    T he

  • 2+ Consecutive line breaks should become 2 line breaks, see vertical spacing below

  • Tabs should be removed.



The goal is:

T he
Fox

Jumped

Over


Note: I tried
summary.replace(/\s\s+/g, ' ');
but it outputted
T he Fox Jumped Over
, which took care of the first part, but totally destroyed the vertical spacing on the second part, as it considers it consecutive spacing. Tabs should also be removed/cleaned up if they exist.

How can I get the goal?

Update: I think I screwed up on part 2, with the # of line breaks, so I updated it. But the goal is what we're after in terms of spacing/formatting.

ndn ndn
Answer
summary.replace(/ +/g, ' ').replace(/\n\n+/g, "\n\n").replace(/\t+/g, '');