I crafted two regular expressions that successfully parse the strings I desire parsed. However, when running the code that includes the regular expressions against Sonar Cloud (quality and security check), I get a warning about performance and security.
Here are the regular expressions I crafted (note the · and - characters):
// yearToYearWithIrrWAndDotRegex parses: · 1998 - 2001, · 2001 - Present
const yearToYearWithIrrWAndDotRegex = /·.*?(19|20)\d{2}.*?-.*?((19|20)\d{2}|Present)?/g;
// yearToYearRegex parses: 1998 - 2001, 2001 - Present
const yearToYearRegex = /(19|20)\d{2}.*?-.*?((19|20)\d{2}|Present)/g;
The Sonar Cloud warning:
Evaluating regular expressions against input strings is potentially an extremely CPU-intensive task. Specially crafted regular expressions such as (a+)+s will take several seconds to evaluate the input string aaaaaaaaaaaaaaaaaaaaaaaaaaaaabs. The problem is that with every additional a character added to the input, the time required to evaluate the regex doubles. However, the equivalent regular expression, a+s (without grouping) is efficiently evaluated in milliseconds and scales linearly with the input size.
TLDR: Is there any way to make my RegEx better?