RegExp
constructors (no-invalid-regexp)The "extends": "eslint:recommended"
property in a configuration file enables this rule.
An invalid pattern in a regular expression literal is a SyntaxError
when the code is parsed, but an invalid string in RegExp
constructors throws a SyntaxError
only when the code is executed.
This rule disallows invalid regular expression strings in RegExp
constructors.
Examples of incorrect code for this rule:
/*eslint no-invalid-regexp: "error"*/
RegExp('[')
RegExp('.', 'z')
new RegExp('\\')
Examples of correct code for this rule:
/*eslint no-invalid-regexp: "error"*/
RegExp('.')
new RegExp
this.RegExp('[')
ECMAScript 6 adds the following flag arguments to the RegExp
constructor:
You can enable these to be recognized as valid by setting the ECMAScript version to 6 in your ESLint configuration.
If you want to allow additional constructor flags for any reason, you can specify them using an allowConstructorFlags
option in .eslintrc
. These flags will then be ignored by the rule regardless of the ecmaVersion
setting.
This rule has an object option for exceptions:
"allowConstructorFlags"
is an array of flagsExamples of correct code for this rule with the { "allowConstructorFlags": ["u", "y"] }
option:
/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["u", "y"] }]*/
new RegExp('.', 'y')
new RegExp('.', 'yu')
This rule was introduced in ESLint 0.1.4.
© JS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/rules/no-invalid-regexp