Google Apps脚本中的转义正则表达式文字 [英] Escape regex literal in Google Apps Script

查看:41
本文介绍了Google Apps脚本中的转义正则表达式文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么它不起作用-我已经通过网上找到的更好的正则表达式工具之一对其进行了验证,并且我使用的似乎是转义字符'\"前加上\使其变成文字"+",但Google Scripts一直抱怨说无效的量词'+'"(第2行)

这是我的脚本,省略了某些个人详细信息(当我使用此帐户作为垃圾邮件过滤器时,这是为了清除电子邮件)

  function getEmailData(){var find_email_regex = new RegExp("myemail(\ +.* |)\ @ gmail.com");var emails = GmailApp.getChatThreads();var process_email =";尝试 {对于(i = 0; i< emails.length; i ++){for(x = 0; x< emails [i] .getTo().length; x ++){如果(emails [i] .getTo()[x] .matches(find_email_regex)){email_triage(电子邮件[i]);休息;}}}}抓住(e){}}函数email_triage(email){var reject_regex = new RegExp("^ [^ \ +] * $");尝试 {如果(email.matches(reject_regex)){email.moveThreadToTrash();}别的 {email.getMessages()[0] .forward("mymainemail@email.ca");}}抓住(e){}} 

有什么明显的理由说明为什么它总是说无效的量词吗?我的目标是正则表达式我的地址的两个变体,因为垃圾邮件发送者可能会发送一封包含大量邮件的电子邮件,而我只想从其中解析出电子邮件以进行进一步处理.

它需要处理:myemail+stuff@gmail.commyemail@gmail.com

因此按原义,但我对此没有运气.请协助,谢谢.

解决方案

您可以使用以下内容:

  RegExp("myemail(\\ +.*?|)@gmail \\.com");^ ^ ^ ^^^ 

  • 您需要对转义字符进行转义(特殊字符需进行两次转义)
  • 转义 com
  • 之前的点
  • 并且无需转义 @
  • 还要使.* 不贪心

I have no idea why this isn't working - I've put it through one of the better regex tools I've found on the web to verify it, and I'm using what seems to be the escape character '\' before the + sign to make it a literal '+', but Google Scripts keeps complaining saying "Invalid Quantifier '+'" (Line 2)

Here's my script, with certain personal details (it's to clean out email as I use this account as a hard spam filter) omitted:

function getEmailData() {
  var find_email_regex = new RegExp("myemail(\+.*|)\@gmail.com");
  var emails = GmailApp.getChatThreads();
  var process_email = "";
  try {
    for (i = 0; i < emails.length; i++) {
      for (x = 0; x < emails[i].getTo().length; x++) {
        if (emails[i].getTo()[x].matches(find_email_regex)) {
          email_triage(emails[i]);
          break;
        }
      }
    }
  } catch (e) {}
}

function email_triage(email) {
  var reject_regex = new RegExp("^[^\+]*$");
  try {
    if (email.matches(reject_regex)) {
      email.moveThreadToTrash();
    }
    else {
      email.getMessages()[0].forward("mymainemail@email.ca");
    }
  } catch (e) {}
}

Is there any obvious reason as to why it keeps saying invalid quantifier? My objective is to regex two variations of my address, since a spammer might send an email with a large list, and I only want to parse my email out of it for further processing.

It needs to handle: myemail+stuff@gmail.com myemail@gmail.com

Hence the literal, but I have no luck with it. Please assist, thank you.

解决方案

You can use the following:

RegExp("myemail(\\+.*?|)@gmail\\.com");
                ^    ^  ^      ^^

  • You need to escape the escape character (double escape for special characters)
  • escape the dot before com
  • and no need to escape @
  • also make the .* non greedy

这篇关于Google Apps脚本中的转义正则表达式文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆