Reputation: 135
So, I have a javascript string which is actually some html markup assigned to it.
Now, I want to remove all the html comments and its content from the string, ie all the occurrences of the opening comment tag and closing comment tag; along with the comment inside in it.
So I want to remove all occurences of
<!-- some comment -->
Please note I want ' some comment ' removed as well...
Can someone help me with the regex to replace this...
Thanks
Upvotes: 4
Views: 14637
Reputation: 3356
You can use this RegEx to replace the text between <!--
and -->
/(\<!--.*?\-->)/g
Check the snippet below
var string = '<!-- some comment --><div><span>Some Content</span></div><!-- some other comment -->';
var reg = /(\<!--.*?\-->)/g;
string = string.replace(reg,"");
console.log(string);
Upvotes: 1
Reputation: 367
i think you are looking for like this.
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>
Upvotes: 1
Reputation: 19154
like this
var str = `<div></div>
<!-- some comment -->
<p></p>
<!-- some comment -->`
str = str.replace(/<\!--.*?-->/g, "");
console.log(str)
Upvotes: 15