Code
HTML comment Regex
HTML comments start with <!-- and end with -->, and they can span many lines. This pattern finds the shortest run between those markers using a lazy [\s\S]*? so it works across line breaks without the s flag and stops at the first closing marker. It is useful for stripping developer notes from templates before publishing, extracting conditional comments, or counting comments in an audit. Because a regex does not understand context, it will also match comment-like text inside script blocks or attribute values. Use an HTML parser if you need to remove comments from untrusted markup reliably.
Pattern (JavaScript)
/<!--[\s\S]*?-->/Matches the text "<!--", then one of the characters \s\S, zero or more times, as few as possible (lazy), then the text "-->".
Examples
Matches
- ✓ <!-- note -->
- ✓ text <!--x--> more
- ✓ <!-- multi line -->
Does not match
- ✗ <!- not a comment ->
- ✗ <!-- unclosed
- ✗ // js comment
- ✗ --> stray <!--
Try it
4 matches (JavaScript engine)
<!-- note --> text <!--x--> more <!-- multi line --> <!- not a comment -> <!-- unclosed // js comment --> stray <!--
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /<!--[\s\S]*?-->/g;
const text = "<!-- note -->";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'<!--[\s\S]*?-->')
text = '<!-- note -->'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/<!--[\s\S]*?-->/';
$text = '<!-- note -->';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("<!--[\\s\\S]*?-->");
String text = "<!-- note -->";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`<!--[\s\S]*?-->`)
text := "<!-- note -->"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"<!--[\s\S]*?-->");
var text = "<!-- note -->";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"<!--[\s\S]*?-->").unwrap();
let text = "<!-- note -->";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /<!--[\s\S]*?-->/
text = '<!-- note -->'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why use [\s\S] instead of a dot?
- In JavaScript a dot does not match line breaks unless the s flag is set. [\s\S] matches any character, including newlines, so multi-line comments work in every flavor.
- Why is the quantifier lazy?
- With a greedy quantifier the match would run from the first <!-- to the last --> in the document, swallowing the markup between separate comments. The lazy version stops at the first closing marker.
- Is it safe to strip comments from user HTML with this?
- Not for security purposes. Browsers parse malformed comments in surprising ways, so a regex can miss content that still renders. Use a proper HTML sanitiser for untrusted input.