Skip to content

Latest commit

 

History

History
65 lines (45 loc) · 1.79 KB

prefer-named-replacement.md

File metadata and controls

65 lines (45 loc) · 1.79 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/prefer-named-replacement
enforce using named replacement
v1.4.0

regexp/prefer-named-replacement

🔧 This rule is automatically fixable by the --fix CLI option.

enforce using named replacement

📖 Rule Details

This rule reports and fixes $n parameter in replacement string that do not use the name of their referenced capturing group.

/* eslint regexp/prefer-named-replacement: "error" */

/* ✓ GOOD */
"abc".replace(/a(?<foo>b)c/, '$<foo>');
"abc".replace(/a(b)c/, '$1');

/* ✗ BAD */
"abc".replace(/a(?<foo>b)c/, '$1');

🔧 Options

{
  "regexp/prefer-named-replacement": ["error", {
      "strictTypes": true
  }]
}
  • strictTypes ... If true, strictly check the type of object to determine if the string instance was used in replace() and replaceAll(). Default is true.
    This option is always on when using TypeScript.

👫 Related rules

🚀 Version

This rule was introduced in eslint-plugin-regexp v1.4.0

🔍 Implementation