Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text inside HTML comment tag?

I have the following HTML:

<!--
<option value="HVAC">HVAC</option>
<option value="Cooling">|---Cooling</option>
<option value="Heating">|---Heating</option>
-->
....

I fetch this file dynamically using jQuery's get method and store it in a string variable named load_types.

How can I strip the HTML comment tags and everything outside of them? I only want the inside HTML:

<option value="HVAC">HVAC</option>
<option value="Cooling">|---Cooling</option>
<option value="Heating">|---Heating</option>

I tried to use the solutions here but nothing worked properly--I just get null as a match.

Thanks for the help!

like image 428
hao_maike Avatar asked May 25 '13 19:05

hao_maike


1 Answers

Please never use regex to parse HTML. You can use the following instead:

var div = $("<div>").html(load_types),
    comment = div.contents().filter(function() {
        return this.nodeType === 8;
    }).get(0);

console.log(comment.nodeValue);

DEMO: http://jsfiddle.net/HHtW7/

like image 56
VisioN Avatar answered Sep 26 '22 08:09

VisioN