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]);