0

I need to get the HTML content which is in the middle of two HTML comments. see the code:

<p>hello</p>
<!-- Write your comments here -->
<p>hello</p>
<!-- another comment -->

In this example, i need to get the <p>Hello</p>, how can i do this? with Regex? Thanks for reading.

2 Answers2

0

You can use the XPath expression //comment() to find the comments, loop over them to find the ones you care about, then loop over the nodes from the first until you find the second.

const comments = document.evaluate("//comment()", document);
let start;
let end;
while (comment = comments.iterateNext()) {
  if (comment.data.trim() === "Write your comments here") {
    start = comment;
  }
  if (comment.data.trim() === "another comment") {
    end = comment;
  }
}

let current = start.nextSibling;
let matches = [];
while (current !== end) {
  matches.push(current);
  current = current.nextSibling;
}

console.log(matches.map(node => node.textContent));
<p>hello</p>
<!-- Write your comments here -->
<p>hello</p>
<!-- another comment -->

Note that this will break if the comments are not siblings, and I've not put any error handling in place.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You can use childNodes and look at the Node type.

function getNodesBetweenComments (wrapper) {
  var state = 0;
  return Array.from(wrapper.childNodes).filter(node => {
    if (node.nodeType === 8) state++;
    else return state === 1 && node.nodeType === 1
  })
}

const wrapper = document.querySelector("#wrapper");
console.log(getNodesBetweenComments(wrapper));


const wrapper2 = document.querySelector("#wrapper2");
console.log(getNodesBetweenComments(wrapper2));
<div id="wrapper">
  <p>hello</p>
  <!-- Write your comments here -->
  <p>hello</p>
  <!-- another comment -->
</div>

<div id="wrapper2">
  <p>hello</p>
  <!-- Write your comments here -->
  <p>hello</p>
  <p>there</p>
  <p>world</p>
  <!-- another comment -->
</div>
epascarello
  • 204,599
  • 20
  • 195
  • 236