useRegexpExec
¥Summary
-
规则生效日期:
v2.3.9¥Rule available since:
v2.3.9 -
诊断类别:
lint/nursery/useRegexpExec¥Diagnostic Category:
lint/nursery/useRegexpExec -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
¥How to configure
{ "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
'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
/thing/.exec('something');¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号