Another possible conceptual problem with stanleysj's approach is that the regex /^\s*\s$/ is looking for a line consisting only of one or more whitespace characters, i.e., is equivalent to /^\s+$/.
This may or may not be what the OPer really wants to terminate the text block with.