{"record":{"id":"be2a6baa9abf3b18","repo":"microsoft/playwright","slug":"error-while-parsing-selector-selector-e-me","errorCode":null,"errorMessage":"Error while parsing selector `${selector}`: ${e.message}","messagePattern":"Error while parsing selector `(.+?)`: (.+?)","errorType":"exception","errorClass":"InvalidSelectorError","httpStatus":null,"severity":"error","filePath":"packages/isomorphic/selectorParser.ts","lineNumber":358,"sourceCode":"      } else if (inClass && next() === ']') {\n        inClass = false;\n      } else if (!inClass && next() === '[') {\n        inClass = true;\n      } else if (!inClass && next() === '/') {\n        break;\n      }\n      source += eat1();\n    }\n    if (eat1() !== '/')\n      syntaxError('parsing regular expression');\n    let flags = '';\n    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions\n    while (!EOL && next().match(/[dgimsuvy]/))\n      flags += eat1();\n    try {\n      return new RegExp(source, flags);\n    } catch (e) {\n      throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\`: ${e.message}`);\n    }\n  }\n\n  function readAttributeToken() {\n    let token = '';\n    skipSpaces();\n    if (next() === `'` || next() === `\"`)\n      token = readQuotedString(next()).slice(1, -1);\n    else\n      token = readIdentifier();\n    if (!token)\n      syntaxError('parsing property path');\n    return token;\n  }\n\n  function readOperator(): AttributeSelectorOperator {\n    skipSpaces();\n    let op = '';","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/isomorphic/selectorParser.ts#L340-L376","documentation":"Thrown when a regex literal inside an attribute selector (e.g. `[attr=/pattern/flags]`) fails to compile via `new RegExp(source, flags)`. The original RegExp constructor error message is wrapped in an InvalidSelectorError. This indicates the regex body or flags are syntactically invalid JavaScript regex.","triggerScenarios":"Using a regex attribute value with an invalid pattern or unsupported flag: `page.locator('text=/.for(/')` (unbalanced paren), `[class=/foo/z/]` (bad flag 'z'), or `[href=/a{2,1/]` (invalid quantifier range). Any `[attr=/.../flags]` where the JS RegExp constructor rejects the source/flags.","commonSituations":"Porting a regex from another engine (lookbehind, recursion, PCRE-only syntax); typos in flags; unescaped delimiters that terminate the regex early leaving invalid flags; regex copied from a comment without the surrounding slashes.","solutions":["Copy the regex body out of the selector and test it in a JS REPL with `new RegExp(body, flags)` to reproduce the underlying SyntaxError.","Fix the regex syntax (balance parens, remove invalid flags, valid quantifier ranges).","Escape any literal '/' inside the pattern or use a character class.","Restrict flags to the allowed set [dgimsuvy]."],"exampleFix":"// before\npage.locator('text=/(foo/g');  // unbalanced group + flag issues\n\n// after\npage.locator('text=/\\(foo\\)/');","handlingStrategy":"validation","validationCode":"function validateRegexAttr(sel: string) {\n  const m = sel.match(/=\\/(.+)\\/([dgimsuvy]*)/);\n  if (m) {\n    try { new RegExp(m[1], m[2]); }\n    catch (e) { throw new Error(`Embedded regex invalid: ${m[1]} / flags ${m[2]}: ${e.message}`); }\n  }\n}\nvalidateRegexAttr(selector);","typeGuard":"function isCompilableRegex(source: string, flags: string): boolean {\n  try { new RegExp(source, flags); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await page.locator(sel).click();\n} catch (e) {\n  if (/Error while parsing selector/.test(e.message) && e.message.includes('/')) {\n    reportInvalidEmbeddedRegex(sel);\n  }\n  throw e;\n}","preventionTips":["Pre-compile any regex you embed in a selector to validate it during development.","Keep regex flags limited to [dgimsuvy].","Store regex patterns as constants and lint them in unit tests."],"tags":["selector","parsing","regex","invalid-selector"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}