Lodash isRegExp and Regex Flags Across Realms
The Lodash utility method _.isRegExp reliably identifies
standard JavaScript regular expressions across different execution
realms (such as iframes or popups) regardless of which regular
expression flags are configured. Because Lodash relies on internal
object tagging rather than prototype inheritance checks, all modern
ECMAScript regular expression flags—including g,
i, m, s, u,
y, d, and v—are successfully
recognized without causing identity mismatch issues.
Cross-Realm Regular Expression Identification
In browser environments, a script often interacts with objects
created in different execution contexts, such as an iframe
or another window. When a regular expression is created in an iframe and
passed to the host window, using instanceof RegExp
evaluates to false because the iframe's
RegExp.prototype does not match the host's
RegExp.prototype.
Lodash resolves this cross-realm boundary issue by using
baseGetTag or
Object.prototype.toString.call(value). Regardless of the
realm in which an object was instantiated, a native regular expression
returns the internal tag [object RegExp].
Supported Regular Expression Flags
Flags alter matching behavior and syntax rules, but they do not alter
the underlying object type. Consequently, _.isRegExp
successfully identifies any regular expression containing any
combination of the following ECMAScript flags across realms:
g(Global search): Matches all occurrences rather than stopping after the first match.i(Case-insensitive search): Ignores character casing during pattern evaluation.m(Multiline): Treats start (^) and end ($) characters as working across multiple lines.s(dotAll): Allows the period character (.) to match line terminators.u(Unicode): Enables Unicode-aware pattern matching and handles 21-bit Unicode code point escapes.y(Sticky): Matches only from the index indicated by thelastIndexproperty in the target string.d(Generate indices): Produces start and end indices for captured sub-patterns.v(Unicode sets): An extension of theuflag offering set notation, string properties, and expanded character classes.
Because regular expression flags simply update internal internal
slots (such as [[OriginalFlags]]) without changing the
internal [[Class]] or [Symbol.toStringTag] to
anything other than RegExp, no combination of flags will
cause _.isRegExp to fail across browser realms.