The 'g' in your example is a modifier. It causes the regex to match all occurrences of a pattern. In this instance, every new line character and any preceding whitespace will get replaced by nothing, throughout the entire string.
Remove the 'g' and it will match the first occurrence only. You may also want to remove the 'n' because 's' matches all whitespace, including new lines.
So, your regex might need to look simply like...
string.replace(/s*/,"");
Does this work for you?