ECMA-262 regular expressions for .NET
A drop-in replacement for System.Text.RegularExpressions that answers as ECMAScript does,
with performance equal to or better than .NET's on equivalent expressions: over UTF-8 and over UTF-16,
with no allocation per match and a build-time source generator.
Built to answer as ECMAScript does
A pattern written for JavaScript, or taken from a JSON Schema, matches the same texts here.
Regex regex = new(@"\b[a-z]+ing\b", "giu");
int count = regex.Count("Sherlock was walking and talking"); // 2
The answers of ECMAScript
ECMAScript 2025: lookbehind, named groups, modifier groups, Unicode property escapes on Unicode 17 data, and the u and v flags. Tested against V8 on 104,359 patterns and 5.1 million texts.
UTF-8 and UTF-16
One matcher over ReadOnlySpan<byte> and over string or ReadOnlySpan<char>, with no transcoding either way and positions in the units of the text.
Zero allocation
Nothing allocated per match once the per-thread working memory has grown. EnumerateMatches, TryMatch, TryReplace and EnumerateSplits are the allocation-free forms of every member.
The shape of .NET's Regex
Regex, Match, Group, Capture, Matches, Replace, Split and EnumerateMatches, so the library drops in where System.Text.RegularExpressions is used.
A source generator
[GeneratedRegex] writes the search for a start and the match as structured code at build time, with the pattern checked by the compiler. Read the guide →
Bounded in time and memory
A backtracking matcher that remembers where a way has failed, an automaton for the patterns that need no backtracking, and a limit of time and of memory on every match.
From pattern to generated code
Put the attribute on a partial method, and the generator writes the matcher as code. The same flags, the same answers, over both encodings.
Get Startedusing Corvus.Ecma262.RegularExpressions;
public static partial class Patterns
{
[GeneratedRegex(@"(?<word>\w+)@(?<host>\w+)\.\w+", "gu")]
public static partial Regex Email();
}
// Over a string
Match m = Patterns.Email().Match("mail sherlock@baker.st");
string host = m.Groups["host"].Value; // "baker"
// Over UTF-8, allocating nothing
ReadOnlySpan<byte> utf8 = "a@b.c and d@e.f"u8;
int n = Patterns.Email().Count(utf8); // 2
The package
One package carries the library and the source generator
Corvus.Regex.Ecma262
The engine, the drop-in surface, the UTF-8 members and the generator. Targets net10.0 and netstandard2.0. Get started →
dotnet add package Corvus.Regex.Ecma262
Ready to get started?
Install the package, write a pattern with ECMA-262's flags, and match over strings or UTF-8.