If you want to ensure the date at the end of the row matches, consider using the anchor character "$" and refining the format for the date pattern.
It's important to note that the regular expression [A-z]
actually matches more than [A-Za-z]
. To make the pattern case insensitive, you can include the flag /i
.
^.* ((?:19|20)\d{2} [A-Z]+ (?:0?[1-9]|[12]\d|3[01]) [A-Z]+)$
Check out a demo of this regex here.
^
Denotes the start of the string
.*
Matches any character 0 or more times until the last space
(
Captures group 1 which contains the date value
(?:19|20)\d{2}
Matches a year starting with 19 or 20 followed by 2 digits and a space
[A-Z]+
Matches one or more uppercase letters A-Z followed by a space
(?:0?[1-9]|[12]\d|3[01])
Matches a day from 1 to 31
[A-Z]+
Matches one or more uppercase letters A-Z followed by a space
)
Closes group 1
$
Denotes the end of the 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]);