Google Apps Script: count occurences of string in HTML query

Viewed 362

I am trying to get the count of occurrences of a string in the text fetched from a website

  var html = UrlFetchApp.fetch('https://www.larvalabs.com/cryptopunks/details/0000').getContentText();
  var offers = html.match('Offered');
  Logger.log(offers);

However I get the following data returned: [Offered] I tried several methods but I do not find much documentation on those I can use to do this task that sounds simple. I add that I tried to parse with XMLservice but some errors in the HTML code makes it fail.

1 Answers

For example, as one method, how about using matchAll()?

Modified script:

var html = UrlFetchApp.fetch('https://www.larvalabs.com/cryptopunks/details/0000').getContentText();
var offers = [...html.matchAll('Offered')];  // or [...html.matchAll(/Offered/g)]
Logger.log(offers.length);
  • When I tested above, 3 is returned.

Note:

  • In this case, the upper- and lowercase letters are distinguished. Please be careful this.

Reference:

Related