Skip to content

Latest commit

 

History

History
71 lines (44 loc) · 1.91 KB

no-invalid-regexp.md

File metadata and controls

71 lines (44 loc) · 1.91 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-invalid-regexp
disallow invalid regular expression strings in `RegExp` constructors
v1.0.0

regexp/no-invalid-regexp

💼 This rule is enabled in the ✅ plugin:regexp/recommended config.

disallow invalid regular expression strings in RegExp constructors

📖 Rule Details

This rule reports invalid regular expression patterns given to RegExp constructors.

/* eslint regexp/no-invalid-regexp: "error" */

/* ✓ GOOD */
RegExp('foo')
RegExp('[a' + ']')

/* ✗ BAD */
RegExp('\\')
RegExp('[a-Z]*')
RegExp('\\p{Foo}', 'u')

const space = '\\s*'
RegExp('=' + space + '+(\\w+)', 'u')

Differences to ESLint's no-invalid-regexp rule

This rule is almost functionally equivalent to ESLint's no-invalid-regexp rule. The only difference is that this rule doesn't valid flags (see no-non-standard-flag).

There are two reasons we provide this rule:

  1. Better error reporting.

    Instead of reporting the whole invalid string, this rule will try to report the exact position of the syntax error.

  2. Better support for complex constructor calls.

    ESLint's rule only validates RegExp constructors called with simple string literals. This rule also supports operations (e.g. string concatenation) and variables to some degree.

🔧 Options

Nothing.

📚 Further reading

🚀 Version

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

🔍 Implementation