I have an indented JSON string e.g.
Regex.Replace(myJSON, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1")
should do it. It makes sure that strings that contain space characters are preserved, and all other space characters are discarded. All JSON keywords (
null) have to be separated by commas or other punctuation so only white-space inside strings needs to be preserved.
The first option
(\"(?:[^\"\\\\]|\\\\.)*\") matches a double quoted string. The
(...) mean that the output is captured and available in the replacement as
[^\"\\\\] matches any character except a double quote or escape character
Since matching occurs left-to-right, the second option,
\s+ will not match space inside a string.
So we match whole strings, and spaces outside strings. In the former case,
$1 is the string token, and in the latter case
$1 is the empty string because group 1 was not used.
This works as intended because
var x=0is different from
x - -(y)is different from