Object is possibly 'null' for regex
I am able to solve this question using non-null assertion operator !
as below
const body = /<body.*?>([\s\S]*)<\/body>/.exec(html)![1];
If you don't want to use the !
operator, one other option could be to use the optional operator ?
and use a default value.
const body = /<body.*?>([\s\S]*)<\/body>/.exec(html)?[1] ?? '';