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
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
Share Improve this question asked Jul 26, 2017 at 11:15 siddubesiddube 1351 gold badge2 silver badges10 bronze badges 5 |3 Answers
Reset to default 15like this
var str = `<div></div>
<!-- some comment -->
<p></p>
<!-- some comment -->`
str = str.replace(/<\!--.*?-->/g, "");
console.log(str)
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>
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);
<!-- some comment -->
is within a variable contentvar text = '<!-- some comment -->';
? – revo Commented Jul 26, 2017 at 11:17text = text.replace(/<\!--.+?-->/sg,"")
!Don't forget to adds
andg
modifiers – mkHun Commented Jul 26, 2017 at 11:18