22 lines
877 B
Plaintext
22 lines
877 B
Plaintext
There are several reasons to use a group in a regular expression:
|
|
|
|
* to change the precedence (e.g. ``++/do(g|or)/++`` will match 'dog' and 'door')
|
|
* to remember parenthesised part of the match in the case of capturing group
|
|
* to improve readability
|
|
|
|
In any case, having an empty group is most probably a mistake. Either it is a leftover after refactoring and should be removed, or the actual parentheses were intended and were not escaped.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
const dateRegex = /^(?:0[1-9]|[12][0-9]|3[01])[- /.](?:0[1-9]|1[012])[- /.](?:19|20)\d\d(?:)$/; // Noncompliant, useless group at the end
|
|
const methodCallRegex = /foo()/; // Noncompliant, will match only 'foo'
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
const dateRegex = /^(?:0[1-9]|[12][0-9]|3[01])[- /.](?:0[1-9]|1[012])[- /.](?:19|20)\d\d$/;
|
|
const methodCallRegex = /foo\(\)/; // OK, matches 'foo()'
|
|
----
|