Formatting is a parse-and-re-serialize round trip
A formatter does not insert line breaks into your text with a regular expression. It parses the document into an in-memory value - objects, arrays, strings, numbers, booleans and null - and then writes that value back out with an indentation setting. That round trip is why formatting is reliable: if the parse succeeds, the output is guaranteed to be valid JSON, because it was produced by a serializer rather than by string surgery.
It also explains the one surprise people hit. Because the text is rebuilt from parsed values rather than edited in place, anything that was not part of the data model disappears. Comments are gone. Original number spelling is gone. The gap between `{ "a":1 }` and `{"a" : 1}` is gone, because both parse to the same value. What survives is exactly the data, and nothing about how it happened to be typed.