A regular expression may arrive from a different execution context. A runtime type check can be more reliable than inspecting a constructor name.
Worked example
import { types } from "node:util";
console.log(types.isRegExp(/node/i));Result
trueImplementation notes
An object with a source property is not necessarily a regular expression. Accept only the runtime types the caller actually supports.
Try it yourself
Compare a real expression with an object that merely has a test method.
Open the Node.js example →Editorial date September 18, 2025 · Published December 6, 2026