biome complexity/useRegexLiterals: fixed output does not parse
Hypothesis Nemotron 3 Super predicted to fire fired 1×
If the flags string contains characters not valid in regex literals (e.g., `new RegExp("a", "xy")`), the literal `/a/xy` is invalid and throws a SyntaxError, whereas the constructor would have thrown
Program
Output
(no stdout)
(no stdout)
- fired
- 1 before · 0 after
- runs
- each version twice, same file name, no network
Judge Nemotron 3 Ultra advisory real · high
The fix rewrites `new RegExp('a', 'xy')` to the invalid regex literal `/a/xy`, which contains illegal flags 'x' and 'y'. The RegExp constructor accepts arbitrary flag strings at runtime (ignoring unknown flags), but regex literals only allow standard flags (g, i, m, s, u, y). This creates a syntax error where the original code ran successfully.
Explanation Nemotron 3 Super unverified
The regex literal `/a/xy` uses invalid flag `x`, which Node.js does not recognize; valid flags are only g, i, m, s, u, y, d. The original `new RegExp('a', 'xy')` throws a runtime error due to invalid flags, caught silently. The rewrite attempts to parse `/a/xy` as a literal, causing a syntax error during parsing—before execution—so the program fails earlier and differently, breaking syntax rather than throwing a catchable exception. This changes when and how the error occurs, altering program behavior.
Reproduce
- file
prog.js(the before program)- minimized program
- r2-p001.js
- evidence sheet
- r2-p001.md
- regression fixture
- r2-p001.js the minimized program with a header stating the observed change, for the tool's test suite
node prog.js bunx @biomejs/biome lint --only=complexity/useRegexLiterals --write prog.js node prog.js
Upstream search GitHub issues Tavily Nemotron 3 Super + Ultra
useRegexLiterals fix · complexity useRegexLiterals regex literal syntax · biome rule new RegExp flag validation · invalid regex flags xy in literal conversion
- related biomejs/biome/issues/11939 same rule but different construct: that report is about double-escaping forward slashes in the pattern, while this one is about preserving invalid flags like 'xy' when converting to a literal
- related biomejs/biome/issues/1704 same rule (useRegexLiterals) and same defect class (autofix produces invalid syntax), but the original report lacks a snippet showing the exact construct (invalid flags)
- related biomejs/biome/issues/5693 same rule (useRegexLiterals) and same defect class (incorrect autofix), but that report concerns backslash escaping in the pattern while this one concerns invalid flags preservation
Details
- program
- Invalid flags in regex literal cause SyntaxError instead of TypeError
- hazard
- If the flags string contains characters not valid in regex literals (e.g., `new RegExp("a", "xy")`), the literal `/a/xy` is invalid and throws a SyntaxError, whereas the constructor would have thrown
- oracle
/home/sun/dev/diaghunt/runs/2026-10-17T20-13-15-620Z-xspf-biome-complexity_useRegexLiterals/r2-p001/min/r2-p001-reuse/fixed.js:2 console.log(/a/xy.test('')); ^ SyntaxError: Invalid regular expression flags at checkSyntax (node:internal/main/check_syntax:84:5) Node.js v26.8.1- run
- 2026-10-17T20-13-15-620Z-xspf-biome-complexity_useRegexLiterals
generated program (5 lines) and the fix's diff
--- orig.js 2026-10-17 16:13:37.696865576 -0400 +++ fixed.js 2026-10-17 16:13:37.977860201 -0400 @@ -1,5 +1,5 @@ try { - console.log(new RegExp('a', 'xy').test('')); + console.log(/a/xy.test('')); } catch (e) { console.log(e.constructor.name + ': ' + e.message); }
rule documentation given to the models
Summary
- Name: useRegexLiterals
- Fix: safe
- Default severity: warn
- Available from version: 1.3.0
- Diagnostic category: lint/complexity/useRegexLiterals
- This rule is recommended
Description
Enforce the use of the regular expression literals instead of the RegExp constructor if possible.
There are two ways to create a regular expression:
- Regular expression literals, e.g., `/abc/u`.
- The RegExp constructor function, e.g., `new RegExp("abc", "u")` .
The constructor function is particularly useful when you want to dynamically generate the pattern,
because it takes string arguments.
Using regular expression literals avoids some escaping required in a string literal,
and are easier to analyze statically.
Examples
Invalid
```js,expect_diagnostic
new RegExp("abc", "u");
```
Valid
```js
/abc/u;
new RegExp("abc", flags);
```