Molunerfinn/PicGo · error

Invalid URL rewrite regex pattern "${rule.match}": ${error i

Error message

Invalid URL rewrite regex pattern "${rule.match}": ${error instanceof Error ? error.message : String(error)}

What it means

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.

Source

Thrown at src/main/events/rpc/routes/albumToolbox/builtIn/changeURL.ts:48

      ignoreCase: raw.ignoreCase === true
    }
  }).filter(rule => rule.match.length > 0)
}

function buildFlags (rule: Pick<IUrlRewriteRule, 'global' | 'ignoreCase'>): string {
  return `${rule.global ? 'g' : ''}${rule.ignoreCase ? 'i' : ''}`
}

function validateRuleOrThrow (rule: IUrlRewriteRule) {
  if (!rule.match.trim() || !rule.replace.trim()) {
    throw new Error(T('ALBUM_URL_REWRITE_TEMP_RULE_REQUIRED'))
  }
  try {
    new RegExp(rule.match, buildFlags(rule))
  } catch (error) {
    const message = `Invalid URL rewrite regex pattern "${rule.match}": ${error instanceof Error ? error.message : String(error)}`
    logger.error(message)
    throw new Error(message)
  }
}

function applyFirstMatchRewrite (ctx: IPicGo, imgItem: ImgInfo, rules: IUrlRewriteRule[]): ImgInfo {
  const imgInfo = {
    imgUrl: imgItem.imgUrl,
    originImgUrl: imgItem.originImgUrl
  }
  PicGoUtils.applyUrlRewriteToImgInfo(imgInfo, rules, {
    log: {
      error: (...args: Parameters<IPicGo['log']['error']>) => ctx.log.error(...args),
      warn: () => ctx.log.warn(T('ALBUM_URL_REWRITE_EMPTY_RESULT_WARN'))
    }
  })
  if (imgInfo.imgUrl === '') return imgItem
  return imgInfo
}

View on GitHub (pinned to 07ec7068a5)

Solutions

  1. Fix the `match` pattern so `new RegExp(pattern, flags)` compiles — escape special characters like [ ] ( ) { } * + ? \\ ^ $ |
  2. Test the pattern in the browser console with new RegExp before submitting
  3. Remove unsupported constructs (named groups/lookbehind on old Electron builds) or upgrade the runtime
  4. If the pattern comes from user input, validate with try { new RegExp(input) } catch at the form level and show inline feedback

Example fix

// before
{ match: 'https://old.com/(img', replace: '$1' } // Invalid: unclosed group
// after
{ match: 'https://old\\.com/(img)', replace: '$1' }
Defensive patterns

Strategy: validation

Validate before calling

function isCompilableRegex(pattern, flags) {
  try { new RegExp(pattern, flags); return true } catch { return false }
}
if (!isCompilableRegex(rule.match, buildFlags(rule))) throw new Error('invalid regex')

Try / catch

try {
  await changeURL(rule)
} catch (e) {
  if (e.message.startsWith('Invalid URL rewrite regex pattern')) {
    showInlineRegexError(e.message)
  } else throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Molunerfinn/PicGo@07ec7068a5 (2026-08-30). Data as JSON: /api/errors/47e006c5432cc07a. Report an issue: GitHub.