A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://eslint.org/docs/latest/rules/prefer-regex-literals below:

prefer-regex-literals - ESLint - Pluggable JavaScript Linter

prefer-regex-literals

Disallow use of the RegExp constructor in favor of regular expression literals

💡 hasSuggestions

Some problems reported by this rule are manually fixable by editor suggestions

Table of Contents

There are two ways to create a regular expression:

The constructor function is particularly useful when you want to dynamically generate the pattern, because it takes string arguments.

When using the constructor function with string literals, don’t forget that the string escaping rules still apply. If you want to put a backslash in the pattern, you need to escape it in the string literal. Thus, the following are equivalent:

new RegExp("^\\d\\.$");

/^\d\.$/;


1
2
3
4
5

In the above example, the regular expression literal is easier to read and reason about. Also, it’s a common mistake to omit the extra \ in the string literal, which would produce a completely different regular expression:

new RegExp("^\d\.$");


1
2
3

When a regular expression is known in advance, it is considered a best practice to avoid the string literal notation on top of the regular expression notation, and use regular expression literals instead of the constructor function.

Rule Details

This rule disallows the use of the RegExp constructor function with string literals as its arguments.

This rule also disallows the use of the RegExp constructor function with template literals without expressions and String.raw tagged template literals without expressions.

The rule does not disallow all use of the RegExp constructor. It should be still used for dynamically generated regular expressions.

Examples of incorrect code for this rule:

Open in Playground


new RegExp("abc");

new RegExp("abc", "u");

RegExp("abc");

RegExp("abc", "u");

new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");

RegExp(`^\\d\\.$`);

new RegExp(String.raw`^\d\.$`);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Examples of correct code for this rule:

Open in Playground


/abc/;

/abc/u;

/\d\d\.\d\d\.\d\d\d\d/;

/^\d\.$/;



new RegExp(pattern);

RegExp("abc", flags);

new RegExp(prefix + "abc");

RegExp(`${prefix}abc`);

new RegExp(String.raw`^\d\. ${suffix}`);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Options

This rule has an object option:

disallowRedundantWrapping

By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a RegExp constructor call. When the option disallowRedundantWrapping is set to true, the rule will also disallow such unnecessary patterns.

Examples of incorrect code for { "disallowRedundantWrapping": true }

Examples of correct code for { "disallowRedundantWrapping": true }

Version

This rule was introduced in ESLint v6.4.0.

Further Reading Resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4