Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→regex-empty-conditional-commentsnext

regex-empty-conditional-comments3.2.3

Regular expression for matching HTML empty conditional comments

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — EMPTYCONDCO…
  • MATCHING GRA…
  • GLOBAL REGEX…
  • API — VERSION
  • Changelog

No dependencies whatsoever. This package declares no dependencies or devDependencies.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • Extract the first empty conditional comment
  • Find all empty conditional comments
  • Ignore conditional comments that contain markup
  • Collect empty conditional comments but skip populated ones
  • Match the revealed form of an empty conditional comment
  • Remove empty conditional comments
  • Use a fresh regular expression for an independent scan

Purpose

emptyCondCommentRegex() creates a regular expression that finds complete legacy HTML conditional comments whose bodies contain only HTML ASCII whitespace. Use it to find or remove empty Microsoft Outlook and Internet Explorer conditional wrappers without deleting punctuation or non-HTML spacing that can carry content.

API — emptyCondCommentRegex()

The main function emptyCondCommentRegex() is imported like this:

The factory returns a fresh native RegExp instance on every call. Each instance has the g and i flags:

  • g finds every matching empty comment in a string.
  • i makes the if and endif tokens case-insensitive.
import { emptyCondCommentRegex } from "regex-empty-conditional-comments";

const regex = emptyCondCommentRegex();

console.log(regex.flags);
// => "gi"

console.log(regex.test("<!--[IF mso]><![ENDIF]-->"));
// => true

Matching grammar

The regex accepts three complete wrapper layouts. In this notation, CONDITION and BODY are placeholders rather than literal text:

<![if CONDITION]>BODY<![endif]>

<!--[if CONDITION]>BODY<![endif]-->

<!--[if CONDITION]><!-->BODY<!--<![endif]-->
<!--[if CONDITION]><!-- -->BODY<!--<![endif]-->

The grammar applies these rules:

  • After if, the opener requires at least one HTML ASCII whitespace character. The remaining condition can contain zero or more characters other than < and ], and it can span lines. The regex identifies the wrapper; it does not validate the legacy Internet Explorer condition expression.
  • An empty BODY can contain only tab (U+0009), line feed (U+000A), form feed (U+000C), carriage return (U+000D), and space (U+0020).
  • The opening and closing wrapper layouts must agree. Partial openers, missing delimiters, and mixed hidden or revealed markers do not match.

Punctuation and other Unicode spacing count as content. For example, none of these bodies are empty:

console.log(emptyCondCommentRegex().test("<!--[if mso]>!!!<![endif]-->"));
// => false

console.log(emptyCondCommentRegex().test("<!--[if mso]>\u00A0<![endif]-->"));
// => false

console.log(emptyCondCommentRegex().test("<!--[if mso]>&nbsp;<![endif]-->"));
// => false

Global regex state

Like every global regular expression, a retained instance has mutable lastIndex state. A successful test() or exec() advances lastIndex, and a failed search resets it to 0. String.prototype.match() and String.prototype.replace() finish a global scan with lastIndex at 0.

Create a fresh instance for each independent manual scan. If you retain an instance, set lastIndex to 0 before scanning a different string:

const retainedRegex = emptyCondCommentRegex();
const comment = "<!--[if mso]><![endif]-->";

console.log(retainedRegex.test(comment));
// => true

console.log(retainedRegex.lastIndex);
// => 25

retainedRegex.lastIndex = 0;

API — version

You can import version:

Permalink to changelogChangelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab