system64
system64

Reputation: 929

Regular Expressions in Google Apps Script

I'm having trouble with regular expression, that works fine on RegExr.com and JS Console. But fails on Google Apps Script.

regex.gs

function parse()
{
  var regExp = /mobileheading=\"End\sDate\"\>[^\<]+\<\/div\>/

  var html = get_html();  
  Logger.log(html.match(regExp));

}

RegExr link - http://regexr.com/3fcsg

The link above, has the the sample text obtained from get_html().

Upvotes: 4

Views: 8887

Answers (2)

m87
m87

Reputation: 4523

You need to set the global flag. So, the following code should work :

function parse() {
    var regExp = /mobileheading=\"End\sDate\"\>[^\<]+\<\/div\>/g;
    var html = get_html();
    Logger.log(html.match(regExp));
}

Upvotes: 2

Amit Agarwal
Amit Agarwal

Reputation: 11268

Your regex looks good. Remember to add the "g" flag to the regex to capture all matches. It might be an issue with the get_html() method itself.

function parse() {
  var regExp = /mobileheading=\"End\sDate\"\>[^\<]+\<\/div\>/g
  var html = HtmlService.createHtmlOutputFromFile("page.html").getContent();
  Logger.log(html.match(regExp));  
}

Upvotes: 6

Related Questions