Wonka
Wonka

Reputation: 8674

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:

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.

Upvotes: 1

Views: 126

Answers (3)

user557597
user557597

Reputation:

Two+ consecutive spaces or line breaks.

Find ((?:[ ]|\r?\n))\1+
Replace $1 or \1 which ever your engine uses.

edit: to remove all tabs as well.
(Note - when tabs match, capture group 1 will be empty.
Result: tabs get removed, not replaced. Use the same replace.
)

Find (?:\t+|((?:[ ]|\r?\n))\1+)
Replace $1 or \1

edit2: 2+ line breaks become 2 line breaks.
(More add on, OP keeps changing it)

Find (?:\t+|((?:[ ](?=[ ])|(?:\r?\n){2}))\1*)
Replace $1 or \1


Requires only 1 pass and is the fastest way

Upvotes: 2

ndnenkov
ndnenkov

Reputation: 36101

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

Upvotes: 5

redneb
redneb

Reputation: 23850

summary.replace(/([ \n\t])\1+/g, '$1');

or if you all white space characters

summary.replace(/(\s)\1+/g, '$1');

This is more efficient as it does a single pass over the string.

Upvotes: 1

Related Questions