35 lines
942 B
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,javascript]
----
/Jack|Peter|/.test('John'); // Noncompliant - returns 'true'
/Jack||Peter/.test('John'); // Noncompliant - returns 'true'
----
=== Compliant solution
[source,javascript]
----
/Jack|Peter/.test('John'); // returns 'false'
----
=== Exceptions
One could use an empty alternation to make a regular expression group optional. Rule will not report on such cases.
[source,javascript]
----
/mandatory(-optional|)/.test('mandatory'); // returns 'true'
/mandatory(-optional|)/.test('mandatory-optional'); // returns 'true'
----
However, if there is a quantifier after the group the issue will be reported as using both `|` and quantifier is redundant.
[source,javascript]
----
/mandatory(-optional|)?/.test('mandatory'); // Noncompliant - using both `|` inside the group and `?` for the group.
----
include::../implementation.adoc[]