unicorn/prefer-regexp-escape
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-regexp-escape": "error",
},
},
];Prefer RegExp.escape() for escaping strings to use in regular expressions.
const escaped = string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');const escaped = string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');import escapeStringRegexp from 'escape-string-regexp';
const escaped = escapeStringRegexp(string);const escaped = RegExp.escape(string);const escaped = string.replace(/[.*+?^$]/g, '\\$&');const escaped = string.replace(/[.*+?^${}()|[\]\\]/gy, '\\$&');Examples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
unicorn/prefer-regexp-escape
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-regexp-escape": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer arrow function properties over methods with a single return.
Prefer JSON imports over reading and parsing JSON files.
Prefer Uint8Array#toBase64() and Uint8Array.fromBase64() over legacy base64 conversions and manual postprocessing.
Prefer Iterator.concat(…) over temporary spread arrays.
Prefer Uint8Array#toHex() and Uint8Array.fromHex() over manual and Buffer hex conversions.