首页 > 解决方案 > 正则表达式查找自定义标签中的所有文本

问题描述

我想创建一个 Gmail 应用程序来捕获此自定义标签中的所有文本<! some text, blah, blah, blah >

标签是 <! text_to_be_captured_here >

我可以使用什么模式。当然,标签中的文本是可变的。

提前致谢。

标签: google-apps-scriptgmailgmail-addons

解决方案


假设您计划使用 Google Apps 脚本创建上述应用程序,因为您在发布问题时包含了google-apps-script标签。

假设您已经输入了文本并将其存储在变量string中,请尝试下面给出的代码:

var string = "Here is some random text that you received as input from your <!Gmail Application>";

var requiredText = "Custom Tag not found!";

if(string.match(/\<\!(.*?)\>/))
{
  requiredText = string.match(/\<\!(.*?)\>/).pop().trim();
}

Logger.log("Text withing custom tag :"+requiredText);

如果日志中的输出是未找到自定义标签!那么您输入的字符串不包含预定义的自定义标签。

如果没有找到自定义标签以外的任何东西!那么这意味着代码正在运行。

希望这可以帮助!


推荐阅读