Webflags: g
HTML Comment
Match HTML comments, including multi-line comments and empty ones.
Try it in RegexProPattern
regexJavaScript
/<!--[\s\S]*?-->/gRaw source: <!--[\s\S]*?-->
How it works
Opens with <!--, then lazily matches any characters (including newlines via [\s\S]) until the first -->. Lazy quantifier prevents greedy spanning across multiple comments.
Examples
Input
<!-- hello -->Matches
<!-- hello -->
Input
<p>keep</p><!-- remove --><span>keep</span>Matches
<!-- remove -->
Input
no comments hereNo match
—Common use cases
- Stripping comments from HTML output
- Build-time template cleanup
- Detecting conditional IE comments
- Static site generator preprocessing
Related concepts
Lazy vs. Greedy Quantifiers
ConceptGreedy quantifiers (*, +) consume as much as possible before backtracking. Lazy quantifiers (*?, +?) consume as little as possible.
Regex Flags in JavaScript: g, i, m, s, u, y
ConceptFlags modify regex behavior globally. g enables global matching, i makes it case-insensitive, m changes anchor behavior, s dots match newlines, u enables Unicode, y is sticky.
How to Match Across Newlines in JavaScript
How-toThe dot doesn't match newlines by default. Use the s (dotall) flag, or build an explicit [\s\S] alternative for engines that predate s.