{"record":{"id":"47e006c5432cc07a","repo":"Molunerfinn/PicGo","slug":"invalid-url-rewrite-regex-pattern-rule-match","errorCode":null,"errorMessage":"Invalid URL rewrite regex pattern \"${rule.match}\": ${error instanceof Error ? error.message : String(error)}","messagePattern":"Invalid URL rewrite regex pattern \"(.+?)\": (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/main/events/rpc/routes/albumToolbox/builtIn/changeURL.ts","lineNumber":48,"sourceCode":"      ignoreCase: raw.ignoreCase === true\n    }\n  }).filter(rule => rule.match.length > 0)\n}\n\nfunction buildFlags (rule: Pick<IUrlRewriteRule, 'global' | 'ignoreCase'>): string {\n  return `${rule.global ? 'g' : ''}${rule.ignoreCase ? 'i' : ''}`\n}\n\nfunction validateRuleOrThrow (rule: IUrlRewriteRule) {\n  if (!rule.match.trim() || !rule.replace.trim()) {\n    throw new Error(T('ALBUM_URL_REWRITE_TEMP_RULE_REQUIRED'))\n  }\n  try {\n    new RegExp(rule.match, buildFlags(rule))\n  } catch (error) {\n    const message = `Invalid URL rewrite regex pattern \"${rule.match}\": ${error instanceof Error ? error.message : String(error)}`\n    logger.error(message)\n    throw new Error(message)\n  }\n}\n\nfunction applyFirstMatchRewrite (ctx: IPicGo, imgItem: ImgInfo, rules: IUrlRewriteRule[]): ImgInfo {\n  const imgInfo = {\n    imgUrl: imgItem.imgUrl,\n    originImgUrl: imgItem.originImgUrl\n  }\n  PicGoUtils.applyUrlRewriteToImgInfo(imgInfo, rules, {\n    log: {\n      error: (...args: Parameters<IPicGo['log']['error']>) => ctx.log.error(...args),\n      warn: () => ctx.log.warn(T('ALBUM_URL_REWRITE_EMPTY_RESULT_WARN'))\n    }\n  })\n  if (imgInfo.imgUrl === '') return imgItem\n  return imgInfo\n}\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/Molunerfinn/PicGo/blob/07ec7068a5512344da093d49a8408fa63ba3421e/src/main/events/rpc/routes/albumToolbox/builtIn/changeURL.ts#L30-L66","documentation":"validateRuleOrThrow compiles `new RegExp(rule.match, buildFlags(rule))` to validate the rewrite pattern. If the pattern is syntactically invalid (unbalanced groups, bad quantifiers, invalid escape sequences), it logs and rethrows a detailed message including the pattern and the underlying RegExp error. This prevents a bad regex from blowing up later mid-rewrite of image URLs.","triggerScenarios":"Submitting an IUrlRewriteRule whose `match` is not a valid JavaScript RegExp — e.g. 'img[' , 'a{2,1}', trailing backslash, or invalid unicode escapes — through the changeURL RPC route.","commonSituations":"Hand-typing regexes in the UI with unescaped special chars; copying PCRE/ripgrep syntax not supported by JS RegExp (e.g. lookbehind on old engines, \\\\p removed variants); pasting glob patterns expecting regex semantics.","solutions":["Fix the `match` pattern so `new RegExp(pattern, flags)` compiles — escape special characters like [ ] ( ) { } * + ? \\\\ ^ $ |","Test the pattern in the browser console with new RegExp before submitting","Remove unsupported constructs (named groups/lookbehind on old Electron builds) or upgrade the runtime","If the pattern comes from user input, validate with try { new RegExp(input) } catch at the form level and show inline feedback"],"exampleFix":"// before\n{ match: 'https://old.com/(img', replace: '$1' } // Invalid: unclosed group\n// after\n{ match: 'https://old\\\\.com/(img)', replace: '$1' }","handlingStrategy":"validation","validationCode":"function isCompilableRegex(pattern, flags) {\n  try { new RegExp(pattern, flags); return true } catch { return false }\n}\nif (!isCompilableRegex(rule.match, buildFlags(rule))) throw new Error('invalid regex')","typeGuard":null,"tryCatchPattern":"try {\n  await changeURL(rule)\n} catch (e) {\n  if (e.message.startsWith('Invalid URL rewrite regex pattern')) {\n    showInlineRegexError(e.message)\n  } else throw e\n}","preventionTips":["Compile the pattern with new RegExp in the form before saving","Escape regex metacharacters when treating user text literally","Test patterns against sample image URLs before applying to the album","Avoid regex dialects not supported by JS (e.g. some PCRE constructs)"],"tags":["regex","validation","url-rewrite","invalid-pattern"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"07ec7068a5512344da093d49a8408fa63ba3421e","analyzedAt":"2026-08-30T01:45:22.289Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}