{"record":{"id":"d390fd0a5f7dca39","repo":"dianping/cat","slug":"regex","errorCode":null,"errorMessage":"{regex}","messagePattern":"\\{regex\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cat-home/src/main/webapp/assets/js/prettify.js","lineNumber":475,"sourceCode":"          } else if (ch0 !== '\\\\') {\n            // TODO: handle letters in numeric escapes.\n            parts[i] = p.replace(\n                /[a-zA-Z]/g,\n                function (ch) {\n                  var cc = ch.charCodeAt(0);\n                  return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';\n                });\n          }\n        }\n      }\n  \n      return parts.join('');\n    }\n  \n    var rewritten = [];\n    for (var i = 0, n = regexs.length; i < n; ++i) {\n      var regex = regexs[i];\n      if (regex.global || regex.multiline) { throw new Error('' + regex); }\n      rewritten.push(\n          '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');\n    }\n  \n    return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');\n  }\n\n  /**\n   * Split markup into a string of source code and an array mapping ranges in\n   * that string to the text nodes in which they appear.\n   *\n   * <p>\n   * The HTML DOM structure:</p>\n   * <pre>\n   * (Element   \"p\"\n   *   (Element \"b\"\n   *     (Text  \"print \"))       ; #1\n   *   (Text    \"'Hello '\")      ; #2","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-home/src/main/webapp/assets/js/prettify.js#L457-L493","documentation":"google-code-prettify's combinePrefixPatterns builds one master RegExp from a language handler's list of token regexes; it refuses regexes carrying the global (/g) or multiline (/m) flags because it rewrites each pattern's source and renumbers capture groups — flags on the parts would change matching semantics once joined. The thrown Error's message is simply the offending regex's string form, which identifies which pattern caused it.","triggerScenarios":"Registering or shipping a language handler (PR.registerLangHandler) whose fallthrough/style patterns include a regex declared with /g or /m, e.g. [PR.PR_STRING, /\"[^\"]*\"/g]. Then prettifying any code block triggers combinePrefixPatterns and this throw.","commonSituations":"Copy-pasting a regex from application code (where /g was needed for exec loops) into a custom prettify language handler; upgrading a custom lang-*.js written against a different prettify version; hand-writing a syntax highlighter plugin for the editor's code viewer.","solutions":["Remove the g and m flags from every regex in the language handler's pattern lists — they are meaningless there (the combined regex is applied manually with exec in a loop).","Identify the culprit from the message: it prints the regex literally (e.g. /foo/g), so search your handler file for that pattern.","If multiline behavior is needed, express it with (^|[^\\n]) constructs or [\\s\\S] instead of the m flag; matching is already done line-aware by the caller."],"exampleFix":"// before\nvar patterns = [ [PR.PR_STRING, /\"[^\"]*\"/g, /'[^']*'/g] ];\nPR.registerLangHandler(PR.createSimpleLexer([], patterns), ['mylang']);\n\n// after\nvar patterns = [ [PR.PR_STRING, /\"[^\"]*\"/, /'[^']*'/] ];\nPR.registerLangHandler(PR.createSimpleLexer([], patterns), ['mylang']);","handlingStrategy":"try-catch","validationCode":"// before registering a handler, strip offending flags is not possible (flags are\n// immutable), so validate the source patterns at authoring time:\nfunction assertPrettifySafe(regexs) {\n  regexs.forEach(function (r) {\n    if (r.global || r.multiline) throw new Error('remove /g or /m from: ' + r);\n  });\n}","typeGuard":"function isPrettifySafeRegex(r) {\n  return r instanceof RegExp && !r.global && !r.multiline;\n}","tryCatchPattern":"try {\n  prettyPrint();\n} catch (e) {\n  if (/^\\/.+\\/[gmi]*[gm]/.test(e.message) || e instanceof Error && String(e.message).indexOf('/') === 0) {\n    console.warn('prettify handler has flagged regex:', e.message);\n  } else throw e;\n}","preventionTips":["Never reuse application regexes with /g in language handlers","Test custom lang handlers by prettifying a sample snippet at load time","Keep custom handler patterns flag-free: no g, no m"],"tags":["prettify","syntax-highlighting","regex","language-handler","configuration"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}