You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将含富内容的正则匹配文档片段替换为‘deleted’?

解决Google Docs中替换含富内容的{{}}包裹片段问题

问题分析

原代码依赖replaceAllText方法实现替换,该方法仅能匹配纯文本内容,无法识别包含图片、复杂格式的富内容片段——因为它只基于文本字符串匹配,完全忽略文档的元素结构。

解决方案思路

要处理包含富内容的{{}}片段,需要遍历文档的元素层级结构:先定位到{{起始标记,然后逐个删除后续元素直到找到}}结束标记,最后在原位置插入"deleted"文本。

代码实现

function replaceRichContentBlocks() {
  const replaceText = "deleted";
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  let elementIndex = 0;

  while (elementIndex < body.getNumChildren()) {
    const element = body.getChild(elementIndex);
    let startFound = false;
    let endFound = false;
    let blockStartIndex = -1;

    // 查找包含{{的文本元素,标记块起始位置
    if (element.getType() === DocumentApp.ElementType.TEXT) {
      const text = element.asText();
      const textContent = text.getText();
      const startPos = textContent.indexOf("{{");
      
      if (startPos !== -1) {
        startFound = true;
        blockStartIndex = elementIndex;
        // 删除{{标记
        text.deleteText(startPos, startPos + 1);
        // 处理同一文本元素内同时存在}}的情况
        const endPos = textContent.indexOf("}}", startPos);
        if (endPos !== -1) {
          text.deleteText(startPos, endPos + 1);
          text.insertText(startPos, replaceText);
          elementIndex++;
          continue;
        }
      }
    }

    // 处理跨元素的富内容块
    if (startFound) {
      let currentIndex = elementIndex + 1;
      while (currentIndex < body.getNumChildren()) {
        const currentElement = body.getChild(currentIndex);
        
        // 检查当前文本元素是否包含}}结束标记
        if (currentElement.getType() === DocumentApp.ElementType.TEXT) {
          const currentText = currentElement.asText();
          const currentTextContent = currentText.getText();
          const endPos = currentTextContent.indexOf("}}");
          
          if (endPos !== -1) {
            // 删除整个块的所有元素
            for (let i = currentIndex; i >= blockStartIndex; i--) {
              body.removeChild(body.getChild(i));
            }
            // 插入替换文本
            body.insertParagraph(blockStartIndex, replaceText);
            endFound = true;
            break;
          }
        }
        // 非文本元素直接删除
        body.removeChild(currentElement);
      }

      // 处理未闭合的{{情况,删除剩余内容并替换
      if (!endFound) {
        for (let i = body.getNumChildren() - 1; i >= blockStartIndex; i--) {
          body.removeChild(body.getChild(i));
        }
        body.insertParagraph(blockStartIndex, replaceText);
      }
      // 重置索引,继续处理后续内容
      elementIndex = blockStartIndex + 1;
    } else {
      elementIndex++;
    }
  }
}

代码说明

  • 遍历文档子元素,定位包含{{的文本元素,标记块的起始位置
  • 对同一文本元素内同时存在{{和}}的场景,直接删除中间内容并替换
  • 对跨元素的富内容块(含图片、其他格式元素),逐个删除后续元素,直到找到包含}}的文本元素,删除整个块后插入替换文本
  • 兼容未闭合的{{场景,删除从起始标记到文档末尾的内容并完成替换

内容的提问来源于stack exchange,提问作者Aleksandr Pomozov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:25:27