JavaScript / ECMAScript

HTML Comment in JS

Match HTML comments, including multi-line comments and empty ones.

Try it in the JS tester →

Pattern

regexJS
<!--[\s\S]*?-->   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("<!--[\\s\\S]*?-->", "g");
const input = "<!-- hello -->";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

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 here

No match

Same pattern, other engines

← Back to HTML Comment overview (all engines)