Regexp remove duplicate whitespace leaving only 1 or 2 occurences

I’m making a simple comment box and I want to remove excess whitespace by replacing the whitespace with only 1 or 2 tokens, whether they be newlines or spaces.

(\s){2,} is what I have so far.
However, I want to leave users with the ability to double-space their comments, and replacing this with $1, the first capture group, would reduce their lines to a single space.

So basically,

If I have 1 space or newline,
then replace it with 1 space or newline.

If I have more than 1 spaces or newlines,
then replace it with exactly 2 spaces or newlines.


Read more here: Source link