I have the following HTML:
我有以下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
.
我使用jQuery的get方法动态获取此文件,并将其存储在名为load_types的字符串变量中。
How can I strip the HTML comment tags and everything outside of them? I only want the inside HTML:
如何删除HTML注释标记以及它们之外的所有内容?我只想要内部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.
我尝试使用这里的解决方案,但没有正常工作 - 我只是得到null作为匹配。
Thanks for the help!
谢谢您的帮助!
1 个解决方案
#1
15
Please never use regex to parse HTML. You can use the following instead:
请不要使用正则表达式来解析HTML。您可以使用以下代码:
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/
演示:http://jsfiddle.net/HHtW7/
#1
15
Please never use regex to parse HTML. You can use the following instead:
请不要使用正则表达式来解析HTML。您可以使用以下代码:
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/
演示:http://jsfiddle.net/HHtW7/