Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 带有replaceAllText的RegEx Google应用程序脚本_Google Apps Script_Google Slides Api - Fatal编程技术网

Google apps script 带有replaceAllText的RegEx Google应用程序脚本

Google apps script 带有replaceAllText的RegEx Google应用程序脚本,google-apps-script,google-slides-api,Google Apps Script,Google Slides Api,我尝试用replaceAllText替换Google幻灯片中的一些文本,如果我提供字符串,效果很好,但是我找不到正则表达式的解决方案 我想改变的是: N=500 我用replaceAllText尝试的正则表达式变体: /N [=, 0-9]*/ "N [=, 0-9]*" "N = [0-9]*" "N = \d*" /N = \d*/ 但什么都没用 如何将replaceAllText与regex一起使用?问题: 不支持正则表达式,但

我尝试用
replaceAllText
替换Google幻灯片中的一些文本,如果我提供字符串,效果很好,但是我找不到正则表达式的解决方案

我想改变的是:

N=500

我用
replaceAllText
尝试的正则表达式变体:

/N [=, 0-9]*/
"N [=, 0-9]*"
"N = [0-9]*"
"N = \d*"
/N = \d*/
但什么都没用

如何将
replaceAllText
与regex一起使用?

问题: 不支持正则表达式,但支持精确的子字符串匹配

解决方案: 您应该改为使用:

查找(模式):返回当前文本范围内与搜索模式匹配的所有范围。搜索区分大小写

代码示例: 例如,如果要查找并替换演示文稿中的所有正则表达式匹配项,可以执行以下操作:

// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0

function findAndReplace() {
  var pres = SlidesApp.getActivePresentation();
  var slides = pres.getSlides();
  const pattern = "N = \\d*";
  slides.forEach(slide => { // Iterate through every slide in the presentation
    slide.getShapes().forEach(shape => { // Iterate through every shape in the slide
      const textRange = shape.getText(); // Get shape text
      const matches = textRange.find(pattern); // Find matches
      matches.forEach(match => match.setText("REPLACED TEXT")); // Replace text
    });               
  });
}
注:
  • 上面代码示例中的表达式(
    N=\\d*
    )有两个反斜杠,因为正如文档所说,
    模式中的任何反斜杠都应该转义。一个可能的替代方案是
    N=[0-9]*