{"id":"5628a32c20be76c7","repo":"sindresorhus/is","slug":"expected-value-which-is-regexp-received-value-o","errorCode":null,"errorMessage":"Expected value which is `RegExp`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `RegExp`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1872,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('primitive', value));\n\t}\n}\n\nexport function assertPromise<T = unknown>(value: unknown, message?: string): asserts value is Promise<T> {\n\tif (!isPromise(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Promise', value));\n\t}\n}\n\nexport function assertPropertyKey(value: unknown, message?: string): asserts value is PropertyKey {\n\tif (!isPropertyKey(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('PropertyKey', value));\n\t}\n}\n\nexport function assertRegExp(value: unknown, message?: string): asserts value is RegExp {\n\tif (!isRegExp(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('RegExp', value));\n\t}\n}\n\nexport function assertSafeInteger(value: unknown, message?: string): asserts value is number {\n\tif (!isSafeInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('safe integer', value));\n\t}\n}\n\nexport function assertSet<T = unknown>(value: unknown, message?: string): asserts value is Set<T> {\n\tif (!isSet(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Set', value));\n\t}\n}\n\nexport function assertSharedArrayBuffer(value: unknown, message?: string): asserts value is SharedArrayBuffer {\n\tif (!isSharedArrayBuffer(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('SharedArrayBuffer', value));","sourceCodeStart":1854,"sourceCodeEnd":1890,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1854-L1890","documentation":"Thrown by `assert.regExp()` (assertRegExp at source/index.ts:1870) when the value is not a RegExp instance per `isRegExp` (getObjectType check for 'RegExp'). The library enforces that only real RegExp objects pass, not pattern strings. The interpolated `is(value)` shows what type was actually received.","triggerScenarios":"Calling `assert.regExp(value)` with a pattern string like `'^foo$'`, a plain object, or `undefined` instead of a `RegExp` instance created via `/.../` literal or `new RegExp()`.","commonSituations":"Reading a regex pattern from JSON or environment config, where it arrives as a string; APIs that accept `string | RegExp` but assert only RegExp; regexes passed across realms (iframes/vm) are still detected correctly since the check uses object-type tagging, so cross-realm is rarely the cause.","solutions":["Wrap string patterns: `new RegExp(pattern)` before passing to the assertion.","If your API legitimately accepts strings or RegExp, branch with `is.regExp(value)` instead of asserting.","Check for accidental `undefined` from a missing config key and fail with a clearer message upstream."],"exampleFix":"// before\nconst pattern = config.filter; // '^foo$' from JSON\nassert.regExp(pattern);\n// after\nconst pattern = new RegExp(config.filter);\nassert.regExp(pattern);","handlingStrategy":"type-guard","validationCode":"if (!(value instanceof RegExp)) {\n  throw new TypeError('Expected a RegExp');\n}","typeGuard":"function isRegExp(value: unknown): value is RegExp {\n  return Object.prototype.toString.call(value) === '[object RegExp]';\n}","tryCatchPattern":"try {\n  assert.regExp(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // got a pattern string or other non-RegExp\n  }\n  throw error;\n}","preventionTips":["Convert user-supplied pattern strings with new RegExp(pattern) (in its own try/catch for syntax errors) before asserting","Prefer is.regExp() for branching; reserve assert.regExp for invariants","Cross-realm RegExp (from iframes/vm) passes toString-based checks but may fail instanceof — use the toString form"],"tags":["typescript","type-assertion","regexp","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}