prefer-regex-literals

禁止使用 RegExp 构造函数来支持正则表达式字面

创建正则表达式有两种方法:

  • 正则表达式字面,例如 /abc/u
  • RegExp 构造函数,例如 new RegExp("abc", "u")RegExp("abc", "u")

当您想要动态生成模式时,构造函数特别有用,因为它接受字符串参数。

当使用带有字符串字面的构造函数时,不要忘记字符串转义规则仍然适用。如果要在模式中放置反斜杠,则需要在字符串字面中对其进行转义。因此,以下是等价的:

new RegExp("^\\d\\.$");

/^\d\.$/;

// matches "0.", "1.", "2." ... "9."

TvKdvxxk+TFfVheajqJGV9pHls0bYXOTaQ3R1/5PhWrk7wDD0YfTafLf9frBw0GKlALxswM7rZbwJnadwLs1eDzHkgSaTp9yyFhkj+y+5znm0FhPxuOCzzyQ5IUFVUhKhQqUkB165fb8cvHwY8bHVcH7PhbYKtbbnnfK97xGspS/m6bwfZnIOp7FoBlDEyxOGaxBq/rgNVQeZqSF6gdrx8km1dkhNMfMhHw5RFUz4eqPN6pqJeVw/Ugl4ZjKDgqNbQzkMciaGZyCqE8pWb8T4g==

new RegExp("^\d\.$");

// equivalent to /^d.$/, matches "d1", "d2", "da", "db" ...

wNR/Xebv/+Cmc0LelfwXLNkf4m5smS+7f7aZmIfeCkw5zgjnb/ESkq0XkuYlXeqtnixQa/61Xh+oLXMN6wJNYRi+zZHolnV1cZrwRgRM21PwJwW5ncy9J/TRBicq5Z0jJl+2Kg2xLIXhVzOKkzuO4+BGBRK8wSO4ywFfpkKay9C68wR36DVgE6JFKhPAug6uVZMw7pQnZ9wcXHSiAjEMcu5WBAFS5DBOKyjJhjSZ9Gw=

规则详情

amK0sYL4lzAWr/LxHRAloDaZRpJkXEB7Lvke/mpCHaRdjhJPHoCPOjiIBiXL7MFPE64YMnqYMkJSxn9v8BGi+hryuc00xkn2+CvaAD6WT3+3a+D7S/+ZYnEfTlc9krXXwNBWpxtXXYS7m43ivdisIQ==

Gup7YVjDdrzVdXTXw9NoYSPUTiWT/Vnniq0r313Bbn5vR+iCxfmnS4Zt77e/SamsNepEBg0hBZAddiduBX3UMHkBknPzLropEB+1X4gtEi0iqeaAasma+JQS2oqMBljZYB/PLsw7YCFnp6Auw3+lICamIa7S3EGAtj1XzQxT32I/gCcez4gHUe/CmY44wIdX5L9oX0JU58ijcCKtS/CpYx1bw2KNwPzkL8yBdw046M0=

1vZ/vWaNXA7ibabtFQUNMO6piKvvSf0shthWFnVzD+kyLKPNr8d5shlI5ikCF8bAqE0I0voEuNIZmjFcJ1NIO8Vcorv0SF/9K4peZG5/xb6++mfJkeH076xnousLyaLqu+cw/EQG5gFB+wJJaQ8XZtiRt/hUbHRnj8K9v7Kem6E=

zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6dSdz0JClE4rOOJ51S8csCM

/*eslint prefer-regex-literals: "error"*/

new RegExp("abc");

new RegExp("abc", "u");

RegExp("abc");

RegExp("abc", "u");

new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");

RegExp(`^\\d\\.



  
  
  prefer-regex-literals - ESLint 中文文档
  
  
  
  
  
  



  
); new RegExp(String.raw`^\d\. prefer-regex-literals - ESLint 中文文档
);

h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwwaWj0QX+JXnab7YJgxSmDw

/*eslint prefer-regex-literals: "error"*/

/abc/;

/abc/u;

/\d\d\.\d\d\.\d\d\d\d/;

/^\d\.$/;

// RegExp constructor is allowed for dynamically generated regular expressions

new RegExp(pattern);

RegExp("abc", flags);

new RegExp(prefix + "abc");

RegExp(`${prefix}abc`);

new RegExp(String.raw`^\d\. ${suffix}`);

选项

ZmISekGbcAWtmV//14mcrLfLIAw6mxSzDDbtsWnEaXF9CZ1nfG32DOC7X6bvDtXM

    u46irZn0yKmdPBabuT9gn8QZsEVXcbs5Lthxm40NbfxoyazGgLpQEpEKS/wjikmanzcrq5djUiNpltua82tsIIA8vdGqBHbJAmwgN6aHDdP3+zuq2bVpHdjVlNTWHNse5p51/pDo9f3CzaditxJgEuE1fO8KvlrWRxVRzPJ7huS2Cb9D7WBJact8Treo67nBw3R+3qL+Tz2/8dfP0Lkjd26M3FvrwalD6S6wpJW1XUc=

disallowRedundantWrapping

4MJGsCSBV7Wimr5FSX87fBm7/SkhhfvIsY9SPGNqjYoMq7nW6cN+ruyZdCdfmN8OBPw7yp5CPqs9xZ1Pkai0imgpU8EzdUOEpqsJKsaxXxT3Z2WMJAGWRpoBBBGqHtwcdzTSnbvJ6d57YzQjojDoHR7POD8RUQLRZCpxaZVYBd5qoEaKpFZ1ZCivbObFtYY/gypF1blCcyEZ7SSp8fmI5T9ZKQhnW48MAEcUjyXbOqcUKPrIpNXyvT/mVtLG84BNkeDKhpNvsnEWo5sMLqJcFXCiaEvozjohhOgfjiFQlBgwPSU4CrROwVsV7w9NG/ILYu6fepOnjc38dW3cVArRVJkzN8uajcroJ2ruoyLc3ubDzD5R1Bd+CF2+XPfIkEhb

DKbpS7Wi/bhq695Y73gj2MMPrWae22LspWoiKtm0DLg7W02XCL1aK9AA0GDe60CrUD59/HFGPle8qccHzGu7LwX7vOReFMsUpeDnD4QcvHyYwou3BdKTOjolEQK+aj8c

/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/

new RegExp(/abc/);

new RegExp(/abc/, 'u');

DKbpS7Wi/bhq695Y73gj2MMPrWae22LspWoiKtm0DLg7W02XCL1aK9AA0GDe60CrXmHnXomBDJoY7L4cOlAziZ3RiLxv8oQO0BvZPxD6m7dng66Sz0PrjUL+zBWbXO/J

/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/

/abc/;

/abc/u;

new RegExp(/abc/, flags);