# useTopLevelRegex

Rule `biome/useTopLevelRegex`. Require regex literals to be declared at the top level.

- Source: biome
- Technologies: JavaScript
- Kind: performance
- Needs type information: no
- Autofix: no
- Reach: 2.1M weekly downloads of presets that enable it
- Trend: +151.7% last quarter
- Rank: #1561 by reach, #65 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| Biome | `performance/useTopLevelRegex` | no | no | group performance, since 1.8.0 | [docs](https://biomejs.dev/linter/rules/use-top-level-regex/) |

## Code examples

### Incorrect: the rule reports this

```js
function foo(someString) {
    return /[a-Z]*/.test(someString)
}
```

### Correct: the rule accepts this

```js
const REGEX = /[a-Z]*/;

function foo(someString) {
    return REGEX.test(someString)
}
```

```js
function foo(str) {
    return /[a-Z]*/g.exec(str)
}
```

Examples from the Biome documentation.

## Enable it

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "performance": {
        "useTopLevelRegex": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Ultracite for Biome](https://lintradar.dev/presets/ultracite-biome.md) | Biome | `ultracite` | 616 | 2.1M | +151.7% |

## Related rules

- [biome/useTailwindSortedClasses](https://lintradar.dev/rules/biome/useTailwindSortedClasses.md): Enforce the sorting of Tailwind CSS classes.
- [biome/useStaticResponseMethods](https://lintradar.dev/rules/biome/useStaticResponseMethods.md): Use static `Response` methods instead of `new Response()` constructor when possible.
- [biome/useSimplifiedLogicExpression](https://lintradar.dev/rules/biome/useSimplifiedLogicExpression.md): Discard redundant terms from logical expressions.
- [biome/useRequiredScripts](https://lintradar.dev/rules/biome/useRequiredScripts.md): Require configured scripts in `package.json`.
- [biome/useNodeAssertStrict](https://lintradar.dev/rules/biome/useNodeAssertStrict.md): Prefer `node:assert/strict` over `node:assert`.
- [biome/useLoneExecutableDefinition](https://lintradar.dev/rules/biome/useLoneExecutableDefinition.md): Require one executable GraphQL definition per file.

---

Web page: https://lintradar.dev/rules/biome/useTopLevelRegex. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
