0

How to mark end of the row when using match() function? From this page I need to pull out "2021 JANUARY 18 MONDAY"

webpage look

sourcecode

When somethng exists after desired string I've used this code:

var page = UrlFetchApp.fetch(link).getContentText();

var date = page.match(/results of (.*?)SOMETHING/m)[1];

but now I can't

2 Answers2

1

Here's how that date format can be matched in a string:

var string = "lorem ipsum 2021 January 18 Monday";
var date = string.match(/(\d{4} [A-z]+ \d{1,2} [A-z]+)/)[0] || null;

console.log(date);
sbgib
  • 5,580
  • 3
  • 19
  • 26
1

To match the date at the end of the row, you could use the $ anchor and make the format for the date like pattern a bit more specific.

Note that [A-z] matches more than [A-Za-z]. To make the pattern case insensitive you could use the /i flag.

^.* ((?:19|20)\d{2} [A-Z]+ (?:0?[1-9]|[12]\d|3[01]) [A-Z]+)$

Regex demo

  • ^ Start of string
  • .* Match 0+ times any char till the last space
  • ( Capture group 1 (This contains the date like value)
    • (?:19|20)\d{2} Match a year 19 or 20 followed by 2 digits and a space
    • [A-Z]+ Match 1+ times an uppercase char A-Z followed by a space
    • (?:0?[1-9]|[12]\d|3[01]) Match a day 1 - 31
    • [A-Z]+ Match 1+ times an uppercase char A-Z followed by a space
  • ) Close group 1
  • $ End of string

var page = "NHL 2020-2021 ratings through results of 2021 JANUARY 18 MONDAY";
var date = page.match(/^.* ((?:19|20)\d{2} [A-Z]+ (?:0?[1-9]|[12]\d|3[01]) [A-Z]+$)/i)
if (date)
  console.log(date[1]);
The fourth bird
  • 154,723
  • 16
  • 55
  • 70