HTML Comment in GO
Match HTML comments, including multi-line comments and empty ones.
Try it in the GO tester →Pattern
regexGO
<!--[\s\S]*?--> (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`<!--[\s\S]*?-->`)
input := `<!-- hello -->`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern 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
—