diff --git a/rules/S6325/javascript/rule.adoc b/rules/S6325/javascript/rule.adoc index 0a37ea0102..d34ba0d9fa 100644 --- a/rules/S6325/javascript/rule.adoc +++ b/rules/S6325/javascript/rule.adoc @@ -1,10 +1,6 @@ == Why is this an issue? -Regular expression literals should be preferred over the `RegExp` constructor calls when the pattern is a literal. Simply using a regular expression literal is more concise and easier to read and does not require escaping like a string literal does. - -Using the `RegExp` constructor is suitable when the pattern is computed dynamically, e.g. when it is provided by the user. - -=== Noncompliant code example +Using regular expression literals is recommended over using the `RegExp` constructor calls if the pattern is a literal. Regular expression literals are shorter, more readable, and do not need to be escaped like string literals. [source,javascript] ---- @@ -15,7 +11,7 @@ new RegExp("\\d+"); new RegExp(`qux|quuz`); ---- -=== Compliant solution +Using the `RegExp` constructor is suitable when the pattern is computed dynamically, for example when it is provided by the user. [source,javascript] ---- @@ -26,3 +22,11 @@ new RegExp(`qux|quuz`); /qux|quuz/; new RegExp(`Dear ${title},`); ---- + +== Resources + +=== Documentation + +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp[MDN - RegExp] + +