Skip to content

useRegexpExec

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"useRegexpExec": "error"
}
}
}
}

¥Description

如果未提供全局标志,则强制使用 RegExp#exec 而不是 String#match

¥Enforce RegExp#exec over String#match if no global flag is provided.

当正则表达式不包含 g 标志时,String#match 的工作方式与 RegExp#exec 相同。始终使用其中一种方式有助于提高代码可读性。

¥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.

RegExp#exec 的速度可能略快于 String#match;这就是我们选择它作为首选用法的原因。

¥RegExp#exec may also be slightly faster than String#match; this is the reason to choose it as the preferred usage.

¥Examples

¥Invalid

invalid.ts
'something'.match(/thing/);
/invalid.ts:1:1 lint/nursery/useRegexpExec ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Prefer RegExp#exec() over String#match() when searching within a string.

> 1 │ ‘something’.match(/thing/);
^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Use RegExp#exec() instead of String#match() for consistent and slightly faster regex matching.

¥Valid

valid.ts
/thing/.exec('something');

¥Related links