in reply to Replace newlines only if not inside braces
And if they are nested see this thread.
And specifically this excellent post by 7stud