I'm using this post as a reference for this question - How do I regex remove whitespace and newlines from a text, except for when they are in a json's string?
I having the following string in a java program:
"stuff\n blah\n--payload {'meh': 'kar\n'}"
I'm looking for a regex to replace the newline characters in the entire string except for the one's within the JSON string. The result I'm expecting is:
"stuff blah --payload {'meh': 'kar\n'}"
The regex referenced in that post works fine for most cases, but replaces the \n
within the JSON string as well. The end result I get is:
"stuff blah --payload {'meh': 'kar'}"
I've been experimenting with the following set of regexes:
^("[^"]*(?:""[^"]*)*")(\n+) // I expected this to be a combination of newline and newline not within double quotes
[\n\r]\s* //Match new lines, and then could possibly negate it to be within double quotes?
But I still can't seem to get the use case where the newline character within a JSON value string won't be ignored. Is there a possible solution?