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:
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:
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:
regex-empty-conditional-comments3.2.3
Regular expression for matching HTML empty conditional comments
No dependencies whatsoever. This package declares no dependencies or devDependencies.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
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
RegExpinstance on every call. Each instance has thegandiflags:gfinds every matching empty comment in a string.imakes theifandendiftokens case-insensitive.import { emptyCondCommentRegex } from "regex-empty-conditional-comments"; const regex = emptyCondCommentRegex(); console.log(regex.flags); // => "gi" console.log(regex.test("<!--[IF mso]><![ENDIF]-->")); // => trueMatching grammar
The regex accepts three complete wrapper layouts. In this notation,
CONDITIONandBODYare placeholders rather than literal text:The grammar applies these rules:
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.BODYcan contain only tab (U+0009), line feed (U+000A), form feed (U+000C), carriage return (U+000D), and space (U+0020).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]> <![endif]-->")); // => falseGlobal regex state
Like every global regular expression, a retained instance has mutable
lastIndexstate. A successfultest()orexec()advanceslastIndex, and a failed search resets it to0.String.prototype.match()andString.prototype.replace()finish a global scan withlastIndexat0.Create a fresh instance for each independent manual scan. If you retain an instance, set
lastIndexto0before 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 —
versionYou can import
version:Permalink to changelogChangelog
Open Changelog