Eliminate newlines in google app script using regex
<p>I'm trying to write part of an add-on for Google Docs that eliminates newlines within selected text using <code>replaceText</code>. The obvious <code>text.replaceText("\n","");</code> gives the error <code>Invalid argument: searchPattern</code>. I get the same error with <code>text.replaceText("\r","");</code>. The following attempts do nothing: <code>text.replaceText("/\n/","");</code>, <code>text.replaceText("/\r/","");</code>. I don't know why Google App Script does not allow for the recognition of newlines in regex.</p>

<p>I am aware that there is an add-on that does this already, but I want to incorporate this function into my add-on.</p>

<p>This error occurs even with the basic </p>

<pre><code>DocumentApp.getActiveDocument().getBody().textReplace("\n","");
</code></pre>

<p>My full function:</p>

<pre><code>function removeLineBreaks() {

var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
    var elements = selection.getRangeElements();
    for (var i = 0; i &lt; elements.length; i++) {
        var element = elements[i];

        // Only deal with text elements

        if (element.getElement().editAsText) {
            var text = element.getElement().editAsText();

            if (element.isPartial()) {
                text.replaceText("\n","");
            }

            // Deal with fully selected text
            else {
                text.replaceText("\n","");
            }
        }
    }
}

// No text selected
else {
    DocumentApp.getUi().alert('No text selected. Please select some text and try again.');
}
</code></pre>

<p>}</p>