I only want the extra whitespace to be replaced with a newline if the whitespace already contains a newline.
If extra whitespace does NOT contain a newline then the extra whitespace should be replaced with a space.
This is at test
and only a test';
echo preg_replace("/(?=\s\s)\s*?(\n)\s*|(\s)\s+/", "$1$2", $xyz);
var xyz = 'Hello, world. \n This is at test\n and only a test';
alert(xyz); // Should replace extra whitespace with 1 space or 1 new line if it contains new line