persona-community-5/.pnpm-store/v3/files/96/b47e05ddc74a76a82acd5405d07bb6abba714d76415ea6237654625be3ebf508fd30449090c69b4816ee1593c4dee906e9f0d7d4a10187d0aa6bb182d96a07
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

53 lines
1.3 KiB
Plaintext

---
description: 'Enforce `RegExp#exec` over `String#match` if no global flag is provided.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/prefer-regexp-exec** for documentation.
`String#match` is defined to work the same as `RegExp#exec` when the regular expression does not include the `g` flag.
Keeping to consistently using one of the two can help improve code readability.
This rule reports when a `String#match` call can be replaced with an equivalent `RegExp#exec`.
> `RegExp#exec` may also be slightly faster than `String#match`; this is the reason to choose it as the preferred usage.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
'something'.match(/thing/);
'some things are just things'.match(/thing/);
const text = 'something';
const search = /thing/;
text.match(search);
```
</TabItem>
<TabItem value="✅ Correct">
```ts
/thing/.exec('something');
'some things are just things'.match(/thing/g);
const text = 'something';
const search = /thing/;
search.exec(text);
```
</TabItem>
</Tabs>
## When Not To Use It
If you prefer consistent use of `String#match` for both with `g` flag and without it, you can turn this rule off.