$_ = 'this is a test string "hello here" and another "hello there" and so on...';
s/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)//g;
JF8gPSAndGhpcyBpcyBhIHRlc3Qgc3RyaW5nICJoZWxsbyBoZXJlIiBhbmQgYW5vdGhlciAiaGVsbG8gdGhlcmUiIGFuZCBzbyBvbi4uLic7CnMvXHMrKD89KD86W14iXSoiW14iXSoiKSpbXiJdKiQpLy9nOwpwcmludCAkXzsK