简体   繁体   中英

How to remove blank paragraphs of a document converted in string?

I have a very large string, which I get using this GAS methods: var myDocText = DocumentApp.openById(docId).getBody().copy().getText(); (the getText() method "Retrieves the contents of the element as a text string.")

And I want to remove the caracters corresponding to the blank paragraphs and new lines of that document converted in a string. How can I do that?

I'm using this GAS body method (which reduces, but doesn't delete all blank lines):

  var myText = DocumentApp.openById(docId).getBody();
  for (var c = 0; c < myText.getNumChildren(); c++){
    var child = myText.getChild(c);
    if ( (child.getType() == DocumentApp.ElementType.PARAGRAPH) && ( (child.getText() == '') || (child.getText() == ' ') ) ) {
      myText.removeChild(child);
      }
  }

But I'd like to use a string solution. I've tried .replace(/^\\s*[\\r\\n]/gm) ; and it didn't work`. Any suggestion?

我会做

myText.split(/(\r\n|\n|\r)/gm).join('');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM