JavaScript Snippet – Remove Empty Lines

Ignoring whitespace characters too.

your_multiline_string = your_multiline_string.replace(/^\s*$\s*/gm, "");

Yes there are stuff after the $,
in m mode where the entire string is considered “one-line” (well pretty much),
and line-ends are more semantics (if you’re coming from perl, at this point you must be throwing your pipe at the screen…)

can be use useful for formatting a multiline query without wrapping things in ' or " characters:

function multiline(s){return s.toString().replace(/\r/g,"##R##").replace(/\n/g,"##N##").replace(/^.*\/\*\+(.*)\+\*\/.*$/, "$1").replace(/##R##/g,"\r").replace(/##N##/g,"\n");}

var query = line(function(){/*+
[spellcheck]      :not([spellcheck="true"])  :not([readonly]):not([type="hidden"]):not([type="radio"]):not([type="button"]):not([type="file"]):not([type="checkbox"]):not([type="image"]):not([type="reset"]):not([type="submit"])
[contentEditable] :not([spellcheck])         :not([contentEditable="false"])
input             :not([spellcheck])         :not([readonly]):not([type="hidden"]):not([type="radio"]):not([type="button"]):not([type="file"]):not([type="checkbox"]):not([type="image"]):not([type="reset"]):not([type="submit"])
textarea          :not([spellcheck])         :not([readonly])
form              :not([spellcheck])
+*/})
.replace(/^\s*$[\s\:]+/gm, "")                 //remove empty lines
.replace(/[\r\n]+/gm,",").replace(/\,+$/gm,"") //newlines to comma
.replace(/\ +\:/mg,":")                        //some padding

and you’ll get the (compatible) string of:

spellcheck]:not([spellcheck="true"]):not([readonly]):not([type="hidden"]):not([type="radio"]):not([type="button"]):not([type="file"]):not([type="checkbox"]):not([type="image"]):not([type="reset"]):not([type="submit"]),[contentEditable]:not([spellcheck]):not([contentEditable="false"]),input:not([spellcheck]):not([readonly]):not([type="hidden"]):not([type="radio"]):not([type="button"]):not([type="file"]):not([type="checkbox"]):not([type="image"]):not([type="reset"]):not([type="submit"]),textarea:not([spellcheck]):not([readonly]),form:not([spellcheck])"

Enjoy!