2021年9月28日 星期二

Indexing of all matches (JavaScript Google Doc App Scripts)

 

function indexingAllMatches() {

  var doc = DocumentApp.getActiveDocument();

  var body = doc.getBody();

  var re = /[3012]/g; // regular expression

  var str = body.getChild(0).asText().getText();

  // str = "The 1author lives 2in Iowa, 1USA."

  while ((match = re.exec(str)) != null) {

      Logger.log("match found at " + match.index);

  }

  // expected return = 4,18,28

}