# unicorn/prefer-string-starts-ends-with

> Require `startsWith()` and `endsWith()` instead of a regex with `^` or `$`

`/^https:/.test(url)` asks whether a string starts with some fixed text. `url.startsWith("https:")` asks the same thing in plain words, and you don't need to escape the text for a regex.

The rule reports `.test()` on a regex literal that's only `^` followed by plain characters, or plain characters followed by `$`. A regex with the `m` flag, or with the `i` flag and letters, isn't reported.

`--fix` rewrites the call when the argument is a name, a string, a property access or a call.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/prefer-string-starts-ends-with" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
const secure = /^https:/.test(url);
const isScript = /\.js$/.test(file);
```

Not reported:

{/* good */}
```js
const secure = url.startsWith("https:");
const isScript = file.endsWith(".js");

const isImage = /\.(png|jpg)$/.test(file);
const isHeader = /^x-/i.test(name);
```
