{"record":{"id":"b9f2cc4bbbe636de","repo":"oxc-project/oxc","slug":"unexpected-control-character","errorCode":null,"errorMessage":"Unexpected control character","messagePattern":"Unexpected control character","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_control_regex.rs","lineNumber":33,"sourceCode":"        .iter()\n        .map(|ch| {\n            let label = match ch.kind {\n                CharacterKind::Octal1 | CharacterKind::Octal2 | CharacterKind::Octal3 => {\n                    format!(\n                        \"'{ch}' is a control character. It looks like a backreference, but there is no corresponding capture group.\"\n                    )\n                }\n                _ => {\n                    // Show the code point since the character itself is not printable\n                    let ch = format!(\"U+{:04X}\", ch.value);\n                    format!(\"'{ch}' is a control character.\")\n                }\n            };\n            ch.span.label(label)\n        })\n        .collect();\n\n    OxcDiagnostic::warn(if count > 1 {\n        \"Unexpected control characters\"\n    } else {\n        \"Unexpected control character\"\n    })\n    .with_help(\n        \"Avoid matching control characters in regular expressions. If intentional, disable this rule for the expression.\",\n    )\n    .with_labels(labels)\n}\n#[derive(Debug, Default, Clone)]\npub struct NoControlRegex;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows control characters and some escape sequences that match\n    /// control characters in regular expressions.\n    ///","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/oxc-project/oxc/blob/36ec0ef2bae567d1a8413ada2fe795f1af1f2785/crates/oxc_linter/src/rules/eslint/no_control_regex.rs#L15-L51","documentation":"Diagnostic from the oxlint rule `no-control-regex`, singular branch (crates/oxc_linter/src/rules/eslint/no_control_regex.rs:33). When a regular expression contains EXACTLY ONE control character (ASCII 0x00–0x1F plus 0x7F), the message is 'Unexpected control character', with a label rendering that character as `\\xNN`, `\\uNNNN`, or `U+XXXX` if unprintable. Invisible control characters in patterns are a classic source of regexes that match nothing the author can see.","triggerScenarios":"A regex literal or RegExp string containing exactly one control character — e.g. `/\\x1F/` (unit separator), a raw pasted 0x7F DEL byte inside a character class, or `/\\x00/` NUL matching. `count == 1` selects the singular message.","commonSituations":"Single escape left behind after cleaning a pasted pattern; matching a vendor log format that terminates lines with 0x03; CSV/EDI parsing with stray control separators; linting generated fixtures that embed one control byte.","solutions":["Delete the stray control character if it is a paste artifact.","If the match is intentional, replace it with an explicit, visible escape and suppress the rule on that line (`// oxlint-disable-next-line no-control-regex`).","Anchor on the surrounding printable pattern instead of the control char when possible (e.g. match the delimiter token you can see).","Sanitize inputs earlier (`str.replace(/[\\x00-\\x1F\\x7F]/g, '')`) so regexes never need to match control characters."],"exampleFix":"// before\nconst end = /\\x03/; // single control character (ETX)\n\n// after\nconst end = /\\u0003/; // explicit, plus inline suppression if required\n// oxlint-disable-next-line no-control-regex","handlingStrategy":"validation","validationCode":"// Gate: any control character (raw or escaped) inside a regex literal\nconst hasControlInRegex = /\\/(?:[^\\/\\n\\\\]|\\\\.)*\\\\x[01][0-9A-Fa-f](?:[^\\/\\n\\\\]|\\\\.)*\\//.test(src) ||\n  /[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]/.test(src);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize input early so patterns never need control-character matching.","Use dedicated parsers/libraries for ANSI, CSV, and binary formats instead of hand-rolled regexes.","Prefer \\uNNNN-style escapes so intent is visible in review."],"tags":["lint","regex","control-characters","invisible-characters","oxlint"],"backgroundTag":"control-character-regex","analyzedSha":"36ec0ef2bae567d1a8413ada2fe795f1af1f2785","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}