JavaScript

JavaScript

Made by DeepSource

Invalid regular expression strings present in RegExp constructors JS-0017

Bug risk
Major

An invalid pattern in a regular expression literal results in a SyntaxError when the code is parsed, but an invalid string in a RegExp constructor throws a SyntaxError only when the code is executed.

To avoid unwated runtime errors, consider refactoring the regex patterns.

Bad Practice

// Runtime error: unterminated character class
RegExp('[')

// Invalid regular expression flag: z
RegExp('.', 'z')

// \ at end of pattern
new RegExp('\\')

Recommended

RegExp('[a-zA-Z0-9]')

RegExp('.', 'g')

new RegExp('\d+')