sonarjs/stateful-regex
eslint-plugin-sonarjs
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"sonarjs/stateful-regex": "error",
},
},
];Regular expressions with the global flag should be used with caution
const datePattern = /\d{4}-\d{2}-\d{2}/g;
datePattern.test('2020-08-06');
datePattern.test('2019-10-10'); // Noncompliant: the regex will return "false" despite the date being well-formed
const str = 'foodie fooled football';
while ((result = /foo*/g.exec(str)) !== null) { // Noncompliant: a regex is defined at each iteration causing an infinite loop
/* ... */
}
const stickyPattern = /abc/gy; // Noncompliant: a regex defined as both sticky and global ignores the global flag
stickyPattern.test(/* ... */);const datePattern = /\d{4}-\d{2}-\d{2}/;
datePattern.test('2020-08-06');
datePattern.test('2019-10-10'); // Compliant
const reg = /foo*/g;
const str = 'foodie fooled football';
while ((result = reg.exec(str)) !== null) { // Compliant
/* ... */
}
const stickyPattern = /abc/y; // Compliant
stickyPattern.test(/* ... */);Examples from the ESLint documentation.
sonarjs/stateful-regex
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/stateful-regex": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
HTTP Strict-Transport-Security policy should not be disabled
Regular expressions should not cause non-linear backtracking
Regular expressions should not cause catastrophic backtracking
Synchronous exception assertions should receive synchronous callbacks