Один из вариантов - использовать Cheerio для анализа HTML-кода и извлечения необходимой информации из каждого элемента:
const cheerio = require('cheerio');
const htmlStr = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<script src="script.src"></script>
</head>
<body>
...
</body>
</html>`;
const $ = cheerio.load(htmlStr);
const headTags = [];
$('head > *').each((_, elm) => {
headTags.push({ name: elm.name, attribs: elm.attribs, text: $(elm).text() });
});
console.log(headTags);
Выход:
[ { name: 'meta', attribs: { charset: 'UTF-8' }, text: '' },
{ name: 'meta',
attribs:
{ name: 'viewport',
content: 'width=device-width, initial-scale=1.0' },
text: '' },
{ name: 'title', attribs: {}, text: 'Document' },
{ name: 'link',
attribs: { rel: 'stylesheet', href: 'style.css' },
text: '' },
{ name: 'link',
attribs:
{ rel: 'shortcut icon',
href: 'favicon.ico',
type: 'image/x-icon' },
text: '' },
{ name: 'script', attribs: { src: 'script.src' }, text: '' } ]