{"record":{"id":"b8becb2e71065efb","repo":"oxc-project/oxc","slug":"prefer-string-startswith-over-a-regex-with-a-caret","errorCode":null,"errorMessage":"Prefer String#startsWith over a regex with a caret.","messagePattern":"Prefer String#startsWith over a regex with a caret\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs","lineNumber":18,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{CallExpression, Expression, MemberExpression, RegExpFlags, RegExpLiteral},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_regular_expression::ast::{BoundaryAssertionKind, Term};\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::Rule,\n};\n\nfn starts_with(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer String#startsWith over a regex with a caret.\").with_label(span)\n}\n\nfn ends_with(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer String#endsWith over a regex with a dollar sign.\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferStringStartsEndsWith;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefer [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith) over using a regex with `/^foo/` or `/foo$/`.\n    ///\n    /// ::: warning\n    /// This rule is deprecated. Prefer the type-aware [`typescript/prefer-string-starts-ends-with`](https://oxc.rs/docs/guide/usage/linter/rules/typescript/prefer-string-starts-ends-with.html) rule instead.\n    /// :::\n    ///","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_string_starts_ends_with.rs#L1-L36","documentation":"This is the `startsWith` diagnostic of oxlint's `unicorn/prefer-string-starts-ends-with` rule. It reports `/^prefix/.test(str)` — a regex anchored at the start with a caret — and recommends `String#startsWith`, which is faster (no regex engine) and communicates intent. Note the rule's doc marks it deprecated in favor of the type-aware `typescript/prefer-string-starts-ends-with` rule.","triggerScenarios":"A `.test()` call whose argument is a regex literal beginning with `^` followed by only literal terms (boundary assertions parsed via `oxc_regular_expression::ast::BoundaryAssertionKind` / `Term`); the regex must be anchored-at-start only, non-inverted, and free of metacharacters that would change `startsWith` equivalence. A fixer (`RuleFix`/`RuleFixer`) can rewrite it to `str.startsWith('prefix')`.","commonSituations":"Prefix checks written by developers who reach for regex out of habit, e.g. `/^https/.test(url)`; teams migrating configs see the deprecation notice and are steered toward the TypeScript variant, which needs type information to know the receiver is a string.","solutions":["Replace `/^prefix/.test(str)` with `str.startsWith('prefix')`.","Migrate to the replacement rule `typescript/prefer-string-starts-ends-with` (requires type-aware linting) and turn the deprecated unicorn rule off.","Run `oxlint --fix` to auto-convert safe cases.","If the regex has anchors plus other constructs (character classes, `m` flag, lookahead), leave it — the rule correctly does not fire or the fix would change meaning."],"exampleFix":"// before\nif (/^https:/.test(url)) { ... }\n\n// after\nif (url.startsWith('https:')) { ... }","handlingStrategy":"validation","validationCode":"// Intent-revealing prefix checks\nif (url.startsWith('https:')) { /* ... */ }\n// CI: npx oxlint --deny-warn unicorn/prefer-string-starts-ends-with src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use startsWith/endsWith for literal anchors; keep regex for classes, lookarounds, and `i`/`m` flags.","Prefer the non-deprecated `typescript/prefer-string-starts-ends-with` in type-aware setups.","startsWith is case-sensitive — normalize case explicitly when migrating `/^foo/i`."],"tags":["oxlint","unicorn","string","regex","startswith","deprecated-rule"],"backgroundTag":"regex-test-to-startswith","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}