{"record":{"id":"6ae06a2f1aa7e599","repo":"badges/shields","slug":"invalid-re2-regex-error-message","errorCode":null,"errorMessage":"Invalid re2 regex: ${error.message}","messagePattern":"Invalid re2 regex: (.+?)","errorType":"validation","errorClass":"InvalidParameter","httpStatus":400,"severity":"error","filePath":"services/dynamic/dynamic-regex.service.js","lineNumber":86,"sourceCode":"  }\n  static defaultBadgeData = { label: 'match' }\n\n  static validate(flags) {\n    if (flags?.split('')?.some(c => VALID_FLAGS.indexOf(c) === -1)) {\n      throw new InvalidParameter({\n        prettyMessage: `Invalid flags, must be one of: ${VALID_FLAGS}`,\n      })\n    }\n  }\n\n  static transform(buffer, search, replace, flags) {\n    // build re2 regex\n    let re2\n    try {\n      re2 = new RE2(search, flags)\n    } catch (error) {\n      if (error instanceof SyntaxError) {\n        throw new InvalidParameter({\n          prettyMessage: `Invalid re2 regex: ${error.message}`,\n        })\n      }\n      throw error\n    }\n\n    // extract value\n    const found = re2.exec(buffer)\n    if (found == null) {\n      throw new InvalidResponse({\n        prettyMessage: 'no result',\n      })\n    }\n\n    // replace if needed\n    return replace !== undefined ? found[0].replace(re2, replace) : found[0]\n  }\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/badges/shields/blob/766fd8bc89a90b8534dc573ab72dec30215ab1ec/services/dynamic/dynamic-regex.service.js#L68-L104","documentation":"Thrown by the dynamic-regex service when new RE2(search, flags) raises a SyntaxError, i.e. the supplied search pattern is not a syntactically valid RE2 regular expression. RE2 is stricter than PCRE in places, so patterns valid in other engines can fail here. SyntaxErrors are converted into a 404-style InvalidParameter badge; other errors are rethrown.","triggerScenarios":"Any badge request whose search parameter fails to compile under RE2 — unbalanced parentheses/brackets, dangling quantifier, unsupported constructs like lookbehind/lookahead ((?=...), (?<=...)), backreferences (\\1), or invalid escape sequences.","commonSituations":"Using PCRE-only features (lookarounds, backreferences) that RE2 does not support; escaping mistakes in URL query strings; hand-edited regex with a missing bracket; switching from a JS regex engine to RE2 without adjusting the pattern.","solutions":["Test the pattern in an RE2-compatible tester before embedding it in the badge URL.","Rewrite lookarounds and backreferences using RE2-supported constructs (non-capturing groups, explicit alternation).","URL-encode the search parameter so regex metacharacters survive query parsing.","Simplify or reduce the regex to the minimal expression needed to match the value."],"exampleFix":"// before\nsearch=\"version\":\\s*\"(\\d+\\.\\d+)\"(?=,)\n// after (RE2: drop lookahead)\nsearch=\"version\":\\s*\"(\\d+\\.\\d+)\"","handlingStrategy":"validation","validationCode":"const RE2 = require('re2');\nfunction validateRegex(pattern, flags = '') {\n  try {\n    new RE2(pattern, flags); // throws SyntaxError if invalid\n    return true;\n  } catch (e) {\n    if (e instanceof SyntaxError) throw new Error(`Invalid re2 regex: ${e.message}`);\n    throw e;\n  }\n}","typeGuard":"function isValidRe2(pattern, flags = '') {\n  try { new (require('re2'))(pattern, flags); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  const badge = await dynamicRegex({ url, search, replace, flags });\n} catch (err) {\n  if (String(err.message).startsWith('Invalid re2 regex:')) {\n    // fix the pattern: remove lookarounds/backreferences, check escaping\n  } else {\n    throw err;\n  }\n}","preventionTips":["Test patterns in an RE2-compatible tester before embedding in a badge URL.","Avoid RE2-unsupported constructs: lookarounds, backreferences, atomic groups.","URL-encode the search parameter so regex metacharacters survive query parsing."],"tags":["regex","re2","validation","bad-request"],"backgroundTag":"invalid-regex","analyzedSha":"766fd8bc89a90b8534dc573ab72dec30215ab1ec","analyzedAt":"2026-08-30T01:40:27.499Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}