Skip to content

Files

Latest commit

 

History

History
27 lines (21 loc) · 621 Bytes

no-invalid-regexp.md

File metadata and controls

27 lines (21 loc) · 621 Bytes

Pattern: Invalid regular expression in RegExp constructor

Issue: -

Description

Using an invalid pattern in RegExp constructor causes a runtime SyntaxError when the code is executed, unlike regexp literals which fail at parse time. Invalid patterns include incorrect syntax or invalid flags.

Examples

Example of incorrect code:

RegExp('[');
RegExp('.', 'z');
new RegExp('\\');
new RegExp('[a-z', 'g');
RegExp('foo', 'bar');

Example of correct code:

RegExp('.');
new RegExp('[a-z]');
RegExp(pattern, 'g');
new RegExp(/foo/g);
this.RegExp('[');  // Not creating RegExp