Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

like image 319
system64 Avatar asked Nov 16 '25 10:11

system64


2 Answers

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));  
}
like image 85
Amit Agarwal Avatar answered Nov 18 '25 05:11

Amit Agarwal


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));
}
like image 39
m87 Avatar answered Nov 18 '25 05:11

m87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!