{"record":{"id":"95f2fa69e4a4d4d1","repo":"microsoft/playwright","slug":"error-while-parsing-selector-selector-canno","errorCode":null,"errorMessage":"Error while parsing selector `${selector}` - cannot use ${operator} in attribute with regular expression","messagePattern":"Error while parsing selector `(.+?)` - cannot use (.+?) in attribute with regular expression","errorType":"exception","errorClass":"InvalidSelectorError","httpStatus":null,"severity":"error","filePath":"packages/isomorphic/selectorParser.ts","lineNumber":414,"sourceCode":"    while (next() === '.') {\n      eat1();\n      jsonPath.push(readAttributeToken());\n      skipSpaces();\n    }\n    // check property is truthy: [enabled]\n    if (next() === ']') {\n      eat1();\n      return { name: jsonPath.join('.'), jsonPath, op: '<truthy>', value: null, caseSensitive: false };\n    }\n\n    const operator = readOperator();\n\n    let value = undefined;\n    let caseSensitive = true;\n    skipSpaces();\n    if (next() === '/') {\n      if (operator !== '=')\n        throw new InvalidSelectorError(`Error while parsing selector \\`${selector}\\` - cannot use ${operator} in attribute with regular expression`);\n      value = readRegularExpression();\n    } else if (next() === `'` || next() === `\"`) {\n      value = readQuotedString(next()).slice(1, -1);\n      skipSpaces();\n      if (next() === 'i' || next() === 'I') {\n        caseSensitive = false;\n        eat1();\n      } else if (next() === 's' || next() === 'S') {\n        caseSensitive = true;\n        eat1();\n      }\n    } else {\n      value = '';\n      while (!EOL && (isCSSNameChar(next()) || next() === '+' || next() === '.'))\n        value += eat1();\n      if (value === 'true') {\n        value = true;\n      } else if (value === 'false') {","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/isomorphic/selectorParser.ts#L396-L432","documentation":"Thrown when an attribute selector uses a regex value (`/.../`) with an operator other than `=`. Regex matching only supports the plain equality operator; substring/prefix/suffix operators (*=, ^=, $=, |=, ~=) are incompatible with regex semantics and are rejected.","triggerScenarios":"Writing `page.locator('[class*=/active/]')`, `[href^=/https/]`, `[data-id$=/\\d+/]`, or any `[attr OP /regex/]` where OP is not `=`.","commonSituations":"Mistakenly combining CSS substring-match operators with Playwright's regex extension; assuming `*=` works like `.*` with a regex; refactor that changed `=` to `*=` without removing the slashes.","solutions":["Replace the substring/prefix/suffix operator with `=` and fold the matching intent into the regex, e.g. `[class=/.*active.*/]`.","If you need a prefix match, anchor the regex: `[href=/^https/]`.","Drop the regex and use the plain operator with a literal string instead."],"exampleFix":"// before\npage.locator('[class*=/active/]');\n\n// after\npage.locator('[class=/.*active.*/]');","handlingStrategy":"validation","validationCode":"function assertNoRegexWithSubstringOp(sel: string) {\n  if (/[\\^$|~*]=\\/.*\\//.test(sel))\n    throw new Error('Regex attribute values may only use the = operator');\n}\nassertNoRegexWithSubstringOp(selector);","typeGuard":"function regexUsesEqualsOnly(sel: string): boolean {\n  return !/(\\*|\\^|\\$|\\||~)=\\/.*\\//.test(sel);\n}","tryCatchPattern":"try {\n  await page.locator(sel).click();\n} catch (e) {\n  if (/cannot use .* in attribute with regular expression/.test(e.message)) {\n    sel = sel.replace(/([*^$|~])=(\\/.*\\/)/, '=$2');\n    // retry once with corrected operator\n  }\n  throw e;\n}","preventionTips":["Remember the rule: regex values require `=` only.","Fold prefix/substring intent into the regex body (e.g. `^`/`.*`).","Code-review selectors that combine slashes with operators."],"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"}