<div class="box"><strong><!-- comment-->Яблоко<!-- /comment--></strong>Текст текст тектс<!-- comment-->Груша<!-- /comment-->Текст текст</div>
function getTextInComments(el) {
return [...el.childNodes].reduce((acc, curr) => {
if (curr.nodeType === Node.ELEMENT_NODE) {
acc.values.push(...getTextInComments(curr));
} else if (curr.nodeType === Node.COMMENT_NODE) {
acc.isComment = !acc.isComment;
} else if (curr.nodeType === Node.TEXT_NODE && acc.isComment) {
acc.values.push(curr.textContent);
}
return acc;
}, {
values: [],
isComment: false
}).values;
}
const arr = getTextInComments(document.querySelector('.box'));
var html = '<div class="box"><strong><!-- comment-->Яблоко<!-- /comment--></strong>Текст текст тектс<!-- comment-->Груша<!-- /comment-->Текст текст</div>';
var arr = html.match(/<!-- comment-->(.*?)<!-- \/comment-->/g).map(function(val){
return val.replace(/<!-- \/?comment-->/g,'');
});
console.log(arr);