{"record":{"id":"18e6fe2589f758b2","repo":"avajs/ava","slug":"t-regex-must-be-called-with-a-regular-expressi","errorCode":null,"errorMessage":"`t.regex()` must be called with a regular expression","messagePattern":"`t\\.regex\\(\\)` must be called with a regular expression","errorType":"validation","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"lib/assert.js","lineNumber":750,"sourceCode":"\n\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\tassertion: 't.false()',\n\t\t\t\tformattedDetails: [formatWithLabel('Value is not `false`:', actual)],\n\t\t\t}));\n\t\t});\n\n\t\tthis.regex = withSkip((string, regex, message) => {\n\t\t\tassertMessage(message, 't.regex()');\n\n\t\t\tif (typeof string !== 'string') {\n\t\t\t\tthrow fail(new AssertionError('`t.regex()` must be called with a string', {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', string)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (!(regex instanceof RegExp)) {\n\t\t\t\tthrow fail(new AssertionError('`t.regex()` must be called with a regular expression', {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [formatWithLabel('Called with:', regex)],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\tif (!regex.test(string)) {\n\t\t\t\tthrow fail(new AssertionError(message, {\n\t\t\t\t\tassertion: 't.regex()',\n\t\t\t\t\tformattedDetails: [\n\t\t\t\t\t\tformatWithLabel('Value must match expression:', string),\n\t\t\t\t\t\tformatWithLabel('Regular expression:', regex),\n\t\t\t\t\t],\n\t\t\t\t}));\n\t\t\t}\n\n\t\t\treturn pass();\n\t\t});\n","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/assert.js#L732-L768","documentation":"AVA throws this fixed-message AssertionError from t.regex() when the second argument is not a RegExp instance (regex instanceof RegExp fails). The assertion requires an actual regular expression; strings like '/abc/' are not accepted. The offending value is shown after 'Called with:'.","triggerScenarios":"t.regex(str, pattern) where pattern is a string ('^foo$'), a regex-like object from another realm (vm/iframe) or a minified/cloned RegExp, null/undefined, or a RegExp-like duck-typed object.","commonSituations":"Loading patterns from JSON/config where they arrive as strings; destructuring mix-ups putting the string in the regex slot; passing patterns across vm boundaries or worker_threads where instanceof fails; simple typo passing arguments in the wrong order.","solutions":["Convert string patterns to RegExp: t.regex(value, new RegExp(pattern)) or pass a /literal/ regex.","Check the argument order: t.regex(string, regex), not t.regex(regex, string).","If the pattern crosses a vm/worker realm, verify it with Object.prototype.toString.call(p) === '[object RegExp]' and reconstruct with new RegExp(p.source, p.flags).","Guard before the call: if (!(regex instanceof RegExp)) normalize or throw a clearer error."],"exampleFix":"// before\nconst pattern = '^v\\\\d+\\\\.\\\\d+$'; // from config JSON\nt.regex(version, pattern); // TypeError-style assertion\n\n// after\nt.regex(version, new RegExp(pattern));","handlingStrategy":"type-guard","validationCode":"if (!(pattern instanceof RegExp)) {\n  pattern = new RegExp(pattern); // accept string patterns from config\n}","typeGuard":"const isRegExp = (v) => Object.prototype.toString.call(v) === '[object RegExp]';\nif (!isRegExp(pattern)) pattern = new RegExp(String(pattern));","tryCatchPattern":"try {\n  t.regex(value, pattern);\n} catch (err) {\n  if (err.message === '`t.regex()` must be called with a regular expression') {\n    pattern = new RegExp(String(pattern));\n    t.regex(value, pattern);\n  } else throw err;\n}","preventionTips":["Convert config/JSON string patterns with new RegExp(pattern) before use.","Keep argument order straight: t.regex(string, regex).","Use Object.prototype.toString instead of instanceof when RegExp values cross vm/worker realms.","Escape user-supplied pattern text before building RegExp objects."],"tags":["assertion","ava","type-error","regexp"],"backgroundTag":"invalid-argument-value","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}