所以,我有一个JavaScript字符串,实际上是一些HTML标记赋给它的。
现在,我想从字符串中删除所有HTML注释及其内容,即所有开头注释标签和结束注释标签的出现;以及其中的注释内容。
因此,我想要删除所有的出现:
<!-- some comment -->
请注意,我希望“一些注释”也被删除...
有人能帮我用正则表达式替换这个吗...
谢谢。
所以,我有一个JavaScript字符串,实际上是一些HTML标记赋给它的。
现在,我想从字符串中删除所有HTML注释及其内容,即所有开头注释标签和结束注释标签的出现;以及其中的注释内容。
因此,我想要删除所有的出现:
<!-- some comment -->
就像这样
var str = `<div></div>
<!-- some comment -->
<p></p>
<!-- some comment -->`
str = str.replace(/<\!--.*?-->/g, "");
console.log(str)
<!--
和 -->
之间的文本。/(\<!--.*?\-->)/g
var string = '<!-- some comment --><div><span>Some Content</span></div><!-- some other comment -->';
var reg = /(\<!--.*?\-->)/g;
string = string.replace(reg,"");
console.log(string);
i
修饰符有什么用途?最好还可以添加s
修饰符。 - undefined var content = jQuery('body').html();
alert(content.match(/<!--.*?-->/g));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<!-- some comment -->
</body>
</html>
<!-- some comment -->
在变量内容var text = '<!-- some comment -->';
中呢? - undefinedtext = text.replace(/<\!--.+?-->/sg,"")
!不要忘记添加s
和g
修饰符。 - undefined