{"record":{"id":"e0f0c74cf8a1a6cc","repo":"gatsbyjs/gatsby","slug":"the-value-for-the-regex-comparator-must-be-an-ins","errorCode":null,"errorMessage":"The value for the $regex comparator must be an instance of RegExp","messagePattern":"The value for the \\$regex comparator must be an instance of RegExp","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/gatsby/src/datastore/in-memory/indexing.ts","lineNumber":780,"sourceCode":"  if (op === `$ne`) {\n    const set = new Set(filterCache.meta.nodesUnordered)\n\n    removeBucketFromSet(filterValue, filterCache, set)\n\n    // TODO: there's probably a more efficient algorithm to do set\n    //       subtraction in such a way that we don't have to resort here\n    return [...set].sort(sortByIds)\n  }\n\n  if (op === `$regex`) {\n    // Note: $glob is converted to $regex so $glob filters go through here, too\n    // Aside from the input pattern format, further behavior is exactly the same.\n\n    // The input to the filter must be a string (including leading/trailing slash and regex flags)\n    // By the time the filter reaches this point, the filterValue has to be a regex.\n\n    if (!(filterValue instanceof RegExp)) {\n      throw new Error(\n        `The value for the $regex comparator must be an instance of RegExp`\n      )\n    }\n    const regex = filterValue\n\n    const arr: Array<IGatsbyNodePartial> = []\n    filterCache.byValue.forEach((nodes, value) => {\n      // TODO: does the value have to be a string for $regex? Can we auto-ignore any non-strings? Or does it coerce.\n      // Note: for legacy reasons partial paths should also be included for regex\n      if (value !== undefined && regex.test(String(value))) {\n        nodes.forEach(node => arr.push(node))\n      }\n    })\n\n    // TODO: we _can_ cache this list as well. Might make sense if it turns out that $regex is mostly used with literals\n    // TODO: it may make sense to first collect all buckets and then to .concat them, or merge sort them\n\n    arr.sort(sortByIds)","sourceCodeStart":762,"sourceCodeEnd":798,"githubUrl":"https://github.com/gatsbyjs/gatsby/blob/e85d62f1779e353eaac1753211629a26d123e26c/packages/gatsby/src/datastore/in-memory/indexing.ts#L762-L798","documentation":"getNodesFromCacheByValue handling of the $regex (and converted $glob) operator requires the filter value to be a RegExp instance so it can match against the indexed bucket values; a non-RegExp value reaching this branch trips this guard.","triggerScenarios":"Thrown at packages/gatsby/src/datastore/in-memory/indexing.ts:780 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a RegExp instance as the value for $regex","For $glob, supply a glob string; it is converted to RegExp internally","Do not send plain strings to $regex in custom API usage"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e85d62f1779e353eaac1753211629a26d123e26c","analyzedAt":"2026-08-26T17:50:09.662Z","contentChangedAt":"2026-08-26T17:50:09.662Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}