Untitled

 avatar
unknown
plain_text
a year ago
712 B
5
Indexable
function extractHtmlTitle(content) {
  const htmlTitleExpression = /<title[^>]*>([^<]+)<\/title>/i;
  const quotedTitleExpression = /Title: "(.*?)"/
  const htmlHeaderExpression = /<h1>(.*?)<\/h1>/i;

  const htmlTitleMatch = content.match(htmlTitleExpression);
  const quotedTitleMatch = content.match(quotedTitleExpression);
  const htmlHeaderMatch = content.match(htmlHeaderExpression);
  const secondIndex = 1;
  const defaultTitle = "Default Title";

  if (htmlTitleMatch) {
    return htmlTitleMatch[secondIndex];
  }
  if (quotedTitleMatch) {
    return quotedTitleMatch[secondIndex];
  }
  if (htmlHeaderMatch) {
    return quotedTitleMatch[secondIndex];
  }
  return defaultTitle;
}
Editor is loading...
Leave a Comment