Regex for HTML Comment
Regex Pattern
<!--[\s\S]*?-->HTML comment block (multi-line safe)
Quick Answer
The regex pattern for html comment is `<!--[\s\S]*?-->`. HTML comment block (multi-line safe). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| <!-- this is a comment --> | ✓ Matches |
| <!-- multi\nline --> | ✓ Matches |
| <!-- unclosed | ✗ No match |
| not a comment | ✗ No match |
Code Examples
javascript
const regex = /<!--[\s\S]*?-->/; const isValid = regex.test(value);
python
import re
pattern = r'<!--[\s\S]*?-->'
if re.match(pattern, value):
print("valid")ruby
pattern = /<!--[\s\S]*?-->/ if value =~ pattern puts "valid" end
php
if (preg_match('/<!--[\s\S]*?-->/', $value)) {
echo "valid";
}java
String pattern = "<!--[\\s\\S]*?-->"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
HTML Tag
Any HTML tag (opening, closing, or self-closing)
HTML Attribute
HTML attribute key-value pair (attr="value")
HTML Self-Closing Tag
Self-closing HTML tags like <br/>, <img/>, <input/>
HTML Entity
HTML character entity (named or numeric)
Semantic HTML5 Tag
HTML5 semantic element tags
XML Namespace
XML namespace declaration (xmlns or xmlns:prefix)