title: How do I remove text between two "tags" in Google Apps Script
body: <p>I am using Google Apps Script to make documents from a "template". This template in Google Docs has the following format:</p>
  
  <pre><code>(Text above the area of interest...) Culpa cillum excepteur cupidatat cillum ex.
  
  {mySpecialText}
  Est consectetur irure non quis sint exercitation velit qui consequat incididunt officia laborum ea veniam.
  {/mySpecialText}
  
  (More ignored text here...) Tempor voluptate irure laboris occaecat enim ipsum consequat velit.
  </code></pre>
  
  <p>The text between the two tags <code>{mySpecialText}</code> and <code>{/mySpecialText}</code> should be deleted, in addition to the tags. How do I go about doing this? My current code shown below removes the tags but not the paragraph in between.</p>
  
  <pre><code>/**
   * @param doc - a `Document` instance.
   * @param sectionId - the name of the tag
   */
  function removeSection (doc, sectionId) {
    var startTag = '{' + sectionId + '}'
    var endTag = '{/' + sectionId + '}'
  
    var body = doc.getBody()
  
    var startElem = body.findText(startTag)
    var endElem = body.findText(endTag)
  
    if (startElem == null) {
      return Logger.log('Couldn\'t find startElem with tag "' + startTag + '"')
    }
    if (endElem == null) {
      return Logger.log('Couldn\'t find endElem with tag "' + endTag + '"')
    }
  
    startElem = startElem.getElement()
    endElem = endElem.getElement()
  
    var toRemove = []
    var currentElement = startElem
  
    while (currentElement !== null &amp;&amp; !isSameElement(currentElement, endElem)) {
      toRemove.push(currentElement)
      currentElement = currentElement.getNextSibling()
    }
  
    toRemove.push(endElem)
  
    for (var i = 0; i &lt; toRemove.length; i++) {
      toRemove[i].removeFromParent()
    }
  }
  </code></pre>