【问题标题】:Method findText with regexp使用正则表达式的方法 findText
【发布时间】:2014-05-20 07:44:36
【问题描述】:

我想使用 findText(searchPattern) 方法在 google 文档中查找一个字符串。我的 searchPattern 是正则表达式,如下所示:

  var doc = DocumentApp.getActiveDocument().getBody();
  var textToHighlight = '';
  textToHighlight = new RegExp(  "this" ); 
  textLocation = doc.findText(textToHighlight);

但它给了我以下错误,而不是搜索文本:

Google Apps 脚本:参数不能为空:提示

我阅读了 Text 类中 findText 方法的文档。这里它将 searchPattern 指定为字符串。有没有其他方法可以使用正则表达式查找文本。
谢谢

【问题讨论】:

    标签: google-apps-script google-docs google-docs-api


    【解决方案1】:

    您必须将字符串传递给此函数,但它会为您将其转换为 RegExp。这类似于区分字符串和正则表达式的 string.match 函数。

    doc.findText("any\\s+regex.*works");

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      相关资源
      最近更新 更多