How to Get the Content of an HTML Comment Using JavaScript

HTML comments are pieces of code that web browsers ignore. They're used to add notes and documentation to HTML code, making it easier to understand and maintain. Comments are written between <!-- and --> tags.

While browsers ignore comments during rendering, JavaScript can access and extract their content using various string manipulation methods.

Syntax

HTML comments follow this syntax:

<!-- Your comment content here -->

Method 1: Using replaceAll() Method

The replaceAll() method can remove comment tags to extract the inner content:

<!DOCTYPE html>
<html>
   <body>
      <p id="result1"></p>
      <p id="result2"></p>
      <script>
         function extractComment(comment) {
            return comment
               .replaceAll("<!--", "")
               .replaceAll("-->", "");
         }
         
         document.getElementById("result1").innerHTML = extractComment("<!--Welcome to JavaScript-->");
         document.getElementById("result2").innerHTML = extractComment("<!--Tutorials Point-->");
      </script>
   </body>
</html>
Welcome to JavaScript
Tutorials Point

Method 2: Using replace() with Regular Expression

The replace() method with regular expressions provides more precise control over comment extraction:

<!DOCTYPE html>
<html>
   <body>
      <p id="output1"></p>
      <p id="output2"></p>
      <script>
         function getCommentContent(comment) {
            return comment.replace(/<!--(.*?)-->/g, "$1");
         }
         
         document.getElementById("output1").innerHTML = getCommentContent("<!--ICC World Cup-->");
         document.getElementById("output2").innerHTML = getCommentContent("<!--Indian Premier League-->");
      </script>
   </body>
</html>
ICC World Cup
Indian Premier League

Method 3: Extracting Comments from DOM

This method demonstrates how to find and display actual HTML comments from the document:

<!DOCTYPE html>
<html>
   <body>
      <h2>Page Content</h2>
      <!-- This is a hidden comment -->
      <!-- Another comment with data -->
      <div id="comments"></div>
      
      <script>
         function findComments() {
            const bodyHtml = document.body.innerHTML;
            const commentRegex = /<!--(.*?)-->/g;
            const matches = bodyHtml.match(commentRegex);
            
            if (matches) {
               const commentContent = matches.map(comment => 
                  comment.replace(/<!--(.*?)-->/, "$1").trim()
               );
               document.getElementById("comments").innerHTML = 
                  "Found comments: " + commentContent.join(", ");
            }
         }
         
         findComments();
      </script>
   </body>
</html>
Found comments: This is a hidden comment, Another comment with data

Comparison

Method Best For Browser Support
replaceAll() Simple string replacement Modern browsers (ES2021+)
replace() with regex Pattern matching All browsers
DOM extraction Finding existing comments All browsers

Key Points

  • Use replaceAll() for simple comment tag removal
  • Regular expressions provide more flexible pattern matching
  • The (.*?) pattern captures content between comment tags
  • Always trim whitespace from extracted content

Conclusion

JavaScript provides multiple methods to extract HTML comment content, from simple string replacement to advanced regex patterns. Choose the method that best fits your specific use case and browser compatibility requirements.

Updated on: 2026-03-15T23:19:01+05:30

773 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements