HTML Comment Regex

<!--[\s\S]*?-->

Match HTML comments including multi-line comments.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern <!--[\s\S]*?-->:

  • \s - Whitespace character
  • \S - Non-whitespace character
  • ? - Optional (zero or one)
  • * - Zero or more
  • [...] - Character class

Examples

Input Result Explanation
<!-- comment --> ✓ Match Single line comment
<!-- multi line --> ✓ Match Multi-line comment

Code Examples

JavaScript

JavaScript
const pattern = /<!--[\s\S]*?-->/;
const text = "<!-- comment -->";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'<!--[\s\S]*?-->'
text = "<!-- comment -->"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/<!--[\s\S]*?-->/';
$text = "<!-- comment -->";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "<!--[\\s\\S]*?-->";
String text = "<!-- comment -->";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Code minification, comment extraction

Frequently Asked Questions

What is the regex pattern for html comment regex?
The regex pattern is: . Match HTML comments including multi-line comments.
How do I use the html comment regex pattern in JavaScript?
In JavaScript: const regex = new RegExp(''); or const regex = //;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'') in Python. Most regex patterns are cross-language compatible.
What are common use cases for html comment regex?
Code minification, comment extraction

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: