Skip to content
Ultracite
Esc
↑↓navigate↵open⌘Jpreview
Research

Regular expressions in strict mode

Why one JavaScript regex can be read with two grammars, what adding the u flag and naming every group would do to 2,338 regexes in twelve codebases, why no project in their history did either, and what coding agents did with a stack-frame cleaner, a router and a translation splitter.

Hayden Bleasel22 min read

A JavaScript regular expression can be read two ways. Without a flag, the engine uses the forgiving grammar browsers have accepted since the 1990s and reads the string one UTF-16 code unit at a time. With the u flag, it reads code points and rejects anything that isn’t valid syntax. ESLint’s require-unicode-regexp asks for u (or its newer extension, v) on every regex, and prefer-named-capture-group asks for every capture group to have a name, as in (?<year>\d{4}). Oxlint implements both, Biome has them as useUnicodeRegex and useNamedCaptureGroup, and Ultracite turns them on along with the rest of the regex rules, which cover useless escapes, test over match, regexes created inside functions and patterns that backtrack.

Nobody wrote down a reason for these two in particular. They’re on because our policy is to turn on every rule a linter implements unless there’s a reason not to. So this post asks whether they earn their place under that policy.

We read where the flag and named groups came from, what the rules were written for, and what research says about either. We found 2,338 regexes in twelve codebases, tested what adding u does to each one, ran each linter’s rules and fixes on them, and read 21,927 commits of their history. Then we handed the regex reports in three real files to Claude Code and Codex through Ultracite’s agent fix.

One flag, two jobs

ES2015 split every regex into two kinds. In the specification’s words, “A Pattern is either a BMP pattern or a Unicode pattern”, depending on whether its flags contain u. A BMP pattern reads the string as 16-bit code units. A Unicode pattern reads code points, so an emoji outside the Basic Multilingual Plane is one character instead of two halves of a surrogate pair.

The flag does a second job. Browsers had always accepted sloppy patterns. /a{/ matches the text a{, and /\:/ matches a colon, because an escape the engine doesn’t know stands for the character itself. The specification writes those leniencies down in Annex B, and Annex B applies only to patterns without u. That was deliberate. TC39 decided in March 2012 that unknown escapes would be errors under u, and the rule was written into the draft in 2014, after a bug report asked to keep \p{…} free for later. Both later extensions were built on that reservation: property escapes such as \p{Letter} arrived in 2018, and the v flag in 2024. As the v flag’s proposal puts it, “Banning the use of unknown escape sequences in u RegExps was a conscious choice”.

So adding u can turn a working regex into a syntax error, and for a regex literal the error isn’t local. An invalid literal is an early error: the whole file is rejected before any of it runs, and for a module, “the Module is never initialized.” One /\:/u stops Node from loading the file, and every module that imports it, before the first line runs. A new RegExp(pattern, "u") with a bad pattern throws only when that line runs.

What u changes in matching is narrower. Without it, . and negated classes see an emoji as two characters, so /^.$/ doesn’t match "😀", and a class that holds an emoji matches either half of it alone. /\p{L}/ without u matches the text p{L}.

With the i flag, the change is subtler. A case-insensitive regex without u compares characters by uppercasing them. With u, it compares Unicode case folds, and exactly two non-ASCII characters start matching ASCII letters: the Kelvin sign, K (U+212A), matches k, and the long s, ſ (U+017F), matches s. Under iu, [a-z], \w and \b all accept them. Valibot, whose lint config required u on every regex, found this in its own validators. Its email check accepted "\u212a\u017f@example.com", and it took four pull requests, in 2025 and 2026, to fix the email, base64 and IP validators.

The v flag is a second migration on top of the first. It adds set operations and properties of strings, and requires ( ) [ ] { } / - \ | to be escaped inside a class. So [\w-], the usual way to match a word character or a hyphen, is a syntax error under v.

Names that keep their numbers

Named groups are older than JavaScript’s. Python’s re module had (?P<name>...) in version 1.5, in the 1990s, along with a verbose mode that allows comments inside a pattern. Python’s reference put the key property in one line: “A symbolic group is also a numbered group”. JavaScript added (?<name>...) in ES2018, and its proposal kept that rule: “Numbered references to the groups are also created, just as for non-named groups.” JavaScript still has no verbose mode. A proposal for one has been at Stage 1 since 2021.

So naming a group is invisible to code that reads it by number. m[1], $1 and destructuring keep working, and split still returns the capture. The one visible change is a groups object on the match, which a replace callback receives as a new last argument.

The other way to satisfy the rule isn’t safe. A non-capturing group, (?:...), leaves the numbering, so every later group moves down one, and split stops returning it:

"ab".replace(/(a)(b)/, "$2$1");         // "ba"
"ab".replace(/(?<first>a)(b)/, "$2$1"); // "ba"
"ab".replace(/(?:a)(b)/, "$2$1");       // "$2b"

"a,b,c".split(/(,)/);   // ["a", ",", "b", ",", "c"]
"a,b,c".split(/(?:,)/); // ["a", "b", "c"]

Proposed as a strict mode

require-unicode-regexp began as a strictness rule, not a Unicode one. Toru Nagashima proposed it in 2018 as a way to turn off Annex B, likening it to ESLint’s rule for JavaScript’s strict mode: “The u flag makes clearer syntax and helps to catch mistakes early.” It shipped in ESLint 5.3.0 that August. Sindre Sorhus proposed prefer-named-capture-group in 2019, and its docs give one reason, that named groups “can improve their readability.”

Neither rule fixes anything in ESLint. When a contributor asked in 2021 for the rule to offer the edit, the maintainers discussed applying it automatically, and Nicholas C. Zakas replied, “This seems like a risky thing to autofix.” ESLint settled on a suggestion that’s offered only when the pattern still parses with the flag. When a user later asked for the docs to warn about i and \w, Milos Djermanovic explained, “That’s why adding the flag is implemented as suggestion rather than autofix.” For named groups, ESLint offers two suggestions side by side, “Add name to capture group.” and “Convert group to non-capturing.” Only the first keeps the numbering.

The careful way to add the flag exists, in eslint-plugin-regexp. Its version of the rule adds u only when it can prove that every character, class and quantifier matches the same strings either way. On Prism, a syntax highlighter built mostly of regexes, Michael Schmidt measured that the proof covered 60% of its 4,236 regexes without u. The rest needed a person.

Few configs ask for either rule. Of nine popular configs we read, only XO enforces both, and XO had the flag rule off from 2018 to 2026 because of the churn it caused. ESLint keeps require-unicode-regexp out of its recommended set, Airbnb turns both rules off, and Google’s style guides don’t mention regexes. Prettier’s maintainers declined to turn on prefer-named-capture-group: “It makes short simple regexps longer and long expressions even longer.” They did turn on require-unicode-regexp.

Ultracite has had both rules since its first ESLint config, in 2022, with no comment saying why. They went away while Ultracite ran only on Biome, which had neither rule, and came back when Ultracite supported ESLint again, at the end of 2025, and as Oxlint and Biome added the rules in 2026. Each day an Oxlint rule came on, we converted our own regexes, naming the groups our code reads and making the rest non-capturing. When we measured, all 88 regexes in Ultracite’s CLI had u.

Portability is the measured case

Nobody has measured whether named groups help people read regexes. The one controlled study of regex comprehension, by Carl Chapman, Peipei Wang and Kathryn Stolee in 2017, had 180 participants work with equivalent ways of writing the same patterns. It found that octal and hex escapes hurt, and that ranges such as [0-9] were often easier than \d, but it didn’t test names, comments or verbose mode. In an earlier survey of 18 Python developers, two-thirds said they always use numbered references, and “No participants preferred named capture groups.”

The best measured case for u is portability. Developers copy regexes between languages, and a 2019 study by James Davis and colleagues found that 15% of regexes behave differently across the languages they tested. Without u, JavaScript reads another language’s \A, \Z or \p{N} as plain letters, with no error. With u, they fail at load time or mean what their author intended. And before linters asked for the flag, almost nobody used it: a study of npm packages in 2018 found it in 48 of 305,691 unique regexes.

2,338 regexes in twelve codebases

We used the same twelve codebases as the earlier posts, each at a pinned commit: eight TypeScript projects (zod, Hono, tRPC, Vite, Astro, Excalidraw, Next.js and VS Code) and four React codebases (shadcn/ui, Radix, React Aria and Supabase). Their source has 1,143,356 lines and 2,338 regexes, and 88% of the regexes are literals. 62% have no flags at all, 98.8% have neither u nor v, 19 have u, and none has v. Nearly a quarter have a capturing group, and 19 name one, 17 of them in Supabase’s SQL parsers.

Most groups aren’t read the way the rule assumes. Of the 544 regexes with a capturing group, nothing reads a group in 139, some group is read in 256, 12 feed split, and 137 pass the match somewhere we couldn’t follow. Only 75 regexes read two or more unnamed groups by index, the case where a reader has to count parentheses to know what m[2] holds.

The three linters report exactly the same regexes: 2,310 for the flag, and 860 reports on 505 regexes for named groups. require-unicode-regexp was the 10th most frequent of the 1,241 rules in our census of Ultracite’s Oxlint presets. Oxlint fixes neither rule, so with Oxlint that’s 27.7 reports per 10,000 lines for a person or an agent to clear.

Unchanged, broken or exotic

For each of the 2,131 regexes whose pattern and flags we could resolve, we compiled a copy with u and ran both versions on three sets of strings: the repo’s own strings at each use, strings generated from the pattern, and Unicode edge cases (emoji, astral characters, lone surrogates, the Kelvin sign and the long s).

With u added Regexes Share
The same result on every string 1,864 87.5%
Different only on the Unicode edge cases 238 11.2%
A syntax error 29 1.4%

None matched differently on the repos’ own strings or on the generated ones. 181 of the 238 that changed have i, so the Kelvin sign and the long s start matching. The rest change on emoji and lone surrogates, mostly for the better: with u, . and negated classes take a whole emoji instead of half of one.

The 29 syntax errors are in six codebases, and 27 of them are literals, so each would stop its module loading. 14 are escapes that u rejects, such as \: in Hono’s router, \" in Supabase’s SQL completion and \-. 12 are a lone { or }, as in Excalidraw’s {{…}} translation placeholders, and 3 are an incomplete quantifier. Removing the escapes that no-useless-escape reports fixes 14 of the 27. The other 13 need an escape added, which no rule suggests. TypeScript’s own regex check catches 21 of the 27, and misses all six of Supabase’s \".

With v instead, 355 regexes (16.7%) fail, 289 of them on a class such as [\w-]. The web shows the same gap: Chrome counts the page loads that run a u regex that would be a syntax error under v, and in 2026 it was about 9%.

Nobody added a flag or a name

We read 21,927 commits: three years each of Next.js and VS Code, and the whole history of the other six corpus codebases. No commit in any of them added a u flag or named a group. One removed u: Next.js dropped a \p{ID_Start} pattern and its flag so its devtools bundle met its browser target. When Vite and Astro adopted eslint-plugin-regexp, they made 33 groups non-capturing. None of the twelve codebases turns on either rule.

The regex bugs these projects did fix were about what a pattern matches. Of 315 candidate commits we read by hand, 185 fix a regex bug. 134 matched the wrong strings, 18 were anchored wrong and 10 backtracked. Only 2 were Unicode bugs, and 1 came from a shared g regex. We read the regexes those fixes removed, at the commit before each fix: require-unicode-regexp reported 99.1% of them, and 98.8% of the other regexes in the same files. A rule that reports nearly every regex can’t point at the buggy ones.

One of the two Unicode bugs shows the limit of the flag. Zod had an emoji regex built from surrogate ranges, and replaced it with \p{Extended_Pictographic} and u. The rule reported the old regex, but adding u to it as written makes it stop matching emoji: "😀" goes from a match to none.

Same reports, different fixes

The three linters agree on every report and differ on every fix. Oxlint has no fix for either rule. ESLint offers suggestions in the editor and never applies them. Biome’s useUnicodeRegex has a fix it marks safe, so ultracite fix on a Biome project adds u with nothing left for anyone to review. On copies of every file with a regex, it added u to 2,023 source regexes, and 233 of them then matched differently on the edge cases. In our calibration cases, /k/i, /^\w+$/i and /^[a-z]+$/i all started matching the Kelvin sign and the long s. Biome checks that the new pattern parses and skips the regexes where it wouldn’t, so no file stopped loading. That check is recent. Until Biome 2.6.7, in November 2026, the fix added u to every literal, and a file with /{/ or /\-/ stopped loading. Ultracite had turned the rule on a little over two weeks before that release.

For named groups, ESLint’s two suggestions are where the risk is. “Add name to capture group.” kept the numbering in every case we found. “Convert group to non-capturing.” changed what the code reads in 368 of the 860 reports, 42.8%. In 318 the group itself was read, in 102 a later index shifted, in 27 a replace callback’s arguments moved, and in 17 split lost a capture.

We also read a sample of 82 reports by hand, and a second rater labeled 36 of them blind. For the flag, adding u was safe for 88% of reports (weighted by how common each kind is), changed only edge cases for 8%, plausibly changed behavior for 3% and broke the code for 1%. For named groups, a name would help a reader in 7.8% of reports, about one in thirteen. 31% are better non-capturing, because nothing reads them, 17% need the capture as it is, and the rest read fine either way. The raters agreed closely (κ 0.91 for the flag, 0.78 for groups), but both are Claude models.

A shared regex remembers

The rules around these two raise a problem the focus rules don’t: state. A regex with g or y keeps a lastIndex between calls, so a shared one used with test alternates between true and false on the same input. ES3 had this problem for every literal, which was one object shared by every evaluation, and ES5 made each evaluation create a new object. The bug still ships wherever a g regex is shared. Node’s REPL looped forever on .load, shadcn’s CLI removed "use client" from only every other file, and Astro’s error stacks dropped every other frame.

Rules that tell you to hoist regexes push code toward that shape. Biome’s useTopLevelRegex reported 1,045 regexes inside functions. React Doctor’s js-hoist-regexp, which only reports a new RegExp with a constant pattern inside a loop, reported none. Biome’s rule skips regexes with g or y, so none of its 1,045 would become stateful if hoisted. The saving is small. V8 compiles a literal’s pattern once per site, and in our microbenchmark a literal inside a function ran as fast as a hoisted one. Only when we kept every new object, so V8 couldn’t skip creating it, did the literal cost about 6 nanoseconds a call more.

One fix does make a regex stateful. In our calibration, Oxlint’s fix for unicorn/prefer-regexp-test, which plain ultracite fix applies, rewrote str.match(COMMA) with a stored /,/g as COMMA.test(str), and the function started alternating. unicorn fixed the same bug in its own rule in 2021 and now only suggests the edit. No file in the corpus hit it.

The performance question that matters more is backtracking, which our post on security rules covers. SonarJS’s super-linear-regex reported 142 regexes, and a pump test confirmed that 103 take quadratic time on a long enough input. But in a hand sample, 1 of 12 could be reached by long untrusted input: VS Code’s color regex, where "hsl(1" followed by 20,000 spaces takes 0.7 seconds. That fits the research. About 1% of unique regexes on npm are super-linear, and only 7% of those are exponential, by Davis and colleagues’ count in 2018.

Three files built to trip an agent

None of our 312 earlier agent runs had a regex rule in its handoff. So in November 2026 we set up a project the way ultracite init leaves it for Oxlint, where both rules are already on, and picked three real files that need different answers.

A, a clean conversion. Next.js’s webpack-module-path.ts strips webpack prefixes from stack frames with two regexes, used only with test and replace(regex, ""). Nothing reads their four groups, so adding u and either naming the groups or making them non-capturing changes nothing. v would stop the module loading, on [\w-], and g would make the second call on the same file name return false.

B, a choice. Hono’s getPattern turns a route label such as :id{[0-9]+} into a name and a matcher:

const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/)
if (match) {
  const cacheKey = `${label}#${next}`
  if (!patternCache[cacheKey]) {
    if (match[2]) {
      patternCache[cacheKey] =
        next && next[0] !== ':' && next[0] !== '*'
          ? [cacheKey, match[1], new RegExp(`^${match[2]}(?=/${next})`)]
          : [label, match[1], new RegExp(`^${match[2]}$`)]

The code reads match[1] and match[2], so the groups have to keep their numbers. \: is a syntax error under u, so the escape has to go first. And the two RegExp calls compile the pattern a user writes inside the braces. With u, a route such as :id{\d+\-\d+}, which works today, would throw when it’s added. The right answer leaves those two for review.

C, the trap. Excalidraw’s Trans component splits a translated string into text, placeholders and tags:

const SPLIT_REGEX = /({{[\w-]+}})|(<[\w-]+>)|(<\/[\w-]+>)/g;
// Used for extracting "location" from "{{location}}"
const KEY_REGEXP = /{{([\w-]+)}}/;

split puts every captured group into its output, which is how the tags and placeholders survive. Making SPLIT_REGEX’s groups non-capturing passes every linter, and "Hello {{audience}}" renders as "Hello ". Adding u without escaping {{ stops the module loading.

Each file went through Ultracite’s agent fix as it ran in November 2026: the prompt, up to three attempts, and the same report a user sees. We filtered the handoff to the regex rules, as if the project linted clean otherwise. We ran each subject four times with Claude Code and four times with Codex, 24 runs, after two trial runs on C. Then we imported every final file in a fresh Node process and called its functions on the projects’ own test inputs, realistic inputs and the Unicode edge cases, twice each so that any state would show.

Right in every run

All 24 final files load and return what the originals returned on every input, and so do the two trial runs. Every run finished in one attempt. No agent wrote v or g, removed a capture the code reads, added u to B’s RegExp calls, added a suppression or hid a flag in a variable.

Every agent escaped before it added the flag. On C, all eight wrote \{\{ and \}\}, added u to all four regexes and named every group, so split still returns the tokens:

-const SPLIT_REGEX = /({{[\w-]+}})|(<[\w-]+>)|(<\/[\w-]+>)/g;
+const SPLIT_REGEX =
+  /(?<key>\{\{[\w-]+\}\})|(?<tagStart><[\w-]+>)|(?<tagEnd><\/[\w-]+>)/gu;
 // Used for extracting "location" from "{{location}}"
-const KEY_REGEXP = /{{([\w-]+)}}/;
+const KEY_REGEXP = /\{\{(?<name>[\w-]+)\}\}/u;

Claude Code said why: “Unicode mode rejects bare { and }, so the literal braces are now escaped as \{\{ and \}\}.” On B, all eight removed all three useless escapes and wrote the same regex, character for character: /^:(?<name>[^{}]+)(?:\{(?<pattern>.+)\})?$/u.

On A, where nothing reads a group, the agents differed on style. Claude Code made all four groups non-capturing in every run: “Nothing reads the captures — both regexes are only used with .test() and .replace(regex, "") — so invented names would be unused.” Codex named them layer, prefix and namespace in three runs, and made them non-capturing in one. Both answers are right.

Every B run left the two RegExp calls for review, with the right reason. One Claude Code run gave an example: “For example, :date{\d{4}\-\d{2}} works now and would become a SyntaxError.” In all eight, Ultracite printed “Fixed 7 of 9 issues” and “2 left for you to decide.”, and exited with an error. That’s the right outcome, and it’s also permanent. Neither rule can be satisfied there without changing which routes Hono accepts, so every later agent fix on the file hands the same two reports over again, unless the project suppresses them.

No agent mentioned that a non-capturing group would shift match[2] or drop split’s tokens. Seven Claude Code runs pointed out the opposite, that names keep the numbers. No agent ran anything to check its edit, as the prompt asks. Claude Code’s 12 runs cost $10.91 in all, a median of 96 cents each, and Codex used a median of 26,200 tokens.

Names nobody reads

Two raters, both Claude models, compared each final file with the one the agent was handed. One saw a blind packet, and called 21 of the 24 files about as easy to follow as before. The 3 it called easier are Codex’s B runs that also changed match[1] and match[2] to names:

const match = label.match(/^:(?<name>[^{}]+)(?:\{(?<pattern>.+)\})?$/u);
if (match?.groups) {
  const { name, pattern } = match.groups;

That edit is the only readability gain both raters agree on, and no rule asked for it. On C, the blind rater noted that the code still reads tagStartMatch[1] and keyMatch[1], “so the names are never used.” The other rater, which had seen the runs, called 11 files easier, A’s named groups and every B file among them. Neither called a file harder.

We then ran Ultracite’s fix passes with Oxlint, ESLint and Biome, safe and unsafe, on the three subjects and on all 24 final files. Without an agent, Oxlint’s pass changes no regex, and its unsafe pass removes B’s useless escapes. Biome’s pass adds u wherever the result parses and skips C’s brace regexes. No pass changed behavior or undid an agent’s choice.

Ultracite’s checks wouldn’t have caught the traps. We wrote each tempting edit into the files by hand and ran the loop’s check on it. A literal that no longer parses comes back from Oxlint as a parse error, so Ultracite would hand it back to the agent. But the non-capturing split regex, a shared g regex and u on B’s calls all clear every report. On C, Ultracite would have printed “Fixed 10 of 10 issues” and exited cleanly, with every tag and placeholder gone.

Every regex looks the same to it

The flag rule can’t tell its reports apart. It reports 98.8% of regexes. Adding the flag changes nothing for nearly nine in ten of them, breaks the module for about one in seventy, and changes the rest only on edge cases. The rule reports all three with the same message, and it reported the regexes later fixed for bugs as often as any others.

A safe fix isn’t an equivalent one. Biome’s fix checks that the new pattern parses, not that it matches the same strings, and under i it often doesn’t. ESLint’s maintainers kept the flag a suggestion for exactly this reason. The only fixer we found that checks meaning is eslint-plugin-regexp’s, and it could prove 60% of one codebase’s regexes.

Other people’s patterns have no answer. A RegExp call that compiles a user’s pattern can’t take u without changing which patterns the program accepts, and neither rule can be told to skip it. In Ultracite’s loop that report stays open for good.

Naming is safe, and the other suggestion isn’t. A name keeps every read working, but helps a reader in about one report in thirteen. ESLint’s other suggestion changes what the code reads in 43% of reports, and nothing in Ultracite’s loop notices when split loses its tokens.

Only one linter reports a broken literal. Oxlint reports a regex literal that u turned into a syntax error, as a parse error. ESLint and Biome don’t report it at all, and TypeScript catches most but not all of them. In production, the module just doesn’t load.

The evidence is thin. Nobody has measured whether named groups help readers, and our hand labels and readability ratings come from Claude models. Our experiment ran four times per agent on each of three small files, none of them with an i flag, so the case-folding change never came up. These agents, with Ultracite’s prompt, never took the tempting step. A smaller model or a bigger file might.

Escape first, name what you read

Write new regexes with u. In new code it costs nothing, turns a stray escape into an error at load time, and makes . and classes treat an emoji as one character. Use v when you need its set operations, and escape - and brackets inside classes when you do.

Prepare an old regex before adding the flag. Remove useless escapes, escape any lone { or }, and look twice at a regex with i that uses \w, [a-z] or \b. If its input can hold the Kelvin sign or the long s, decide which answer you want.

Review an added u like any other change. When a fix adds the flag, read the diff as you would a hand edit, and run the tests or TypeScript’s regex check if they cover the file.

Leave other people’s patterns alone. Don’t add u to a new RegExp that compiles a pattern your users write. Suppress the report on that line with a comment saying why, or turn the rule off for that file. When an agent leaves it for review, that’s the right answer.

Name the groups you read, and drop the rest. Name a group the code reads, and make a group nothing reads non-capturing. Never make a group non-capturing if something reads it by number, through a replacement string or through split.

Hoist only regexes without g or y. Moving a literal out of a function saves nanoseconds. Moving a global one shares its lastIndex, so keep it where it’s used, or reset lastIndex before each use.

Under the strictness policy, the flag rule mostly asks for a letter that changes nothing, and now and then for one that stops a module loading. Its value is in new code, where it turns typos into errors. The named-group rule is safe to satisfy by naming and risky to satisfy any other way. In our runs, both agents knew the difference: they escaped before adding the flag, named the groups the code reads, and left users’ patterns for a person.

References

The language

The rules

Research

Bugs and fixes

The subjects

Ship less slop

One command sets up your linter, formatter, editor and agents. Works with Oxlint, Biome and ESLint.