33 lines
1019 B
Plaintext

== Why is this an issue?
`String.match()` behaves the same way as `RegExp.exec()` when the regular expression does not include the global flag `g`. While they work the same, `RegExp.exec()` can be slightly faster than `String.match()`. Therefore, it should be preferred for better performance.
The rule reports an issue on a call to `String.match()` whenever it can be replaced with semantically equivalent `RegExp.exec()`.
== How to fix it
Rewrite the pattern matching from `string.match(regex)` to `regex.exec(string)`.
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
----
'foo'.match(/bar/);
----
==== Compliant solution
[source,javascript,diff-id=1,diff-type=compliant]
----
/bar/.exec('foo');
----
== Resources
=== Documentation
* https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/match[String.match()]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec[RegExp.exec()]