9

Using google scripts I'm trying to match a string part that always looks like this: *YYYYMMDD;hhmm* for example: *20170701;0900*

I defined this regex:

var regExp = ('(?:\*)(?P<date>\d+)(?:\;)(?P<time>\d+)(?:\*)','gi');

and then call it using:

var datepart = textbody.match(regExp);

However I'm not getting any match, although the same text in https://regex101.com/ works well. Any idea what I'm doing wrong?

nubie
  • 93
  • 1
  • 1
  • 4
  • 1
    Can you show us what you are actually trying match in its original context? – Tim Biegeleisen Jul 16 '17 at 13:50
  • Thanks Tim. I have a Google scripts function aimed to output the date stamp I enter manually when drafting a message to be sent at a later stage. The element the regex is scanning is a string: var textbody = Utilities.newBlob(part.body.data).getDataAsString(); The String contains a the custom time stamp in the format \*20170710;0900\* followed by the actual text body of the email. I would like to extract the time stamp, store it as variable, and replace with blanks so that the email going out does not contain the timestamp. – nubie Jul 16 '17 at 16:55
  • You created a regex for PCRE engine, while in Google Apps scripts, you should use one for JavaScript – Wiktor Stribiżew Jul 16 '17 at 17:18

1 Answers1

11

You created a regex for PCRE engine, while in Google Apps scripts, you should use one for JavaScript.

Remove all named capturing groups (they are not supported in JS, i.e. (?P<date>\d+) => (\d+)), use a regex literal (i.e. RegExp("pattern", "gi") => /pattern/gi, but i is not necessary here, only use it if there are letters in the pattern), remove the global modifier to get a match with capturing groups intact.

var rx = /\*(\d+);(\d+)\*/;
var datepart = textbody.match(rx); 
var date, time;
if (datepart) {
    date = datepart[1];
    time = datepart[2];
}

Note that (?:\*) = \* because a non-capturing group is still a consuming pattern (i.e. what it matches is added to the match value). Since you want to get subparts of the regex, you just need to focus on the capturing groups, those (...) parts.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    thanks, works still like a charm in 2019, for everyone dont forget to use ECMAScript matching when testing on regex101.com etc. – Marek Čech Jun 22 '19 at 21:31
  • How can we put each captured/enclosed element of our RegEx as a separeted element for a 2D array? https://stackoverflow.com/questions/61022214/google-script-match-regex-into-2d-array – Luis Alberto Delgado de la Flo Apr 04 '20 at 01:49
  • @LuisAlbertoDelgadodelaFlo To get parts of a regex match, you need to enclose those parts with capturing groups, pairs of unescaped parentheses. However, if you use `match(/regex/g)`, with `g` global modifier, you [lose those captured substrings](http://stackoverflow.com/questions/39529921). So, you either use `RegExp#exec` in a loop (as in the [How do you access the matched groups in a JavaScript regular expression?](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) thread), or `matchAll`. – Wiktor Stribiżew Apr 04 '20 at 08:55