Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
712 B
1
Indexable
Never
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;
}
Leave a Comment