Skip to content

Regex

Definition

Namespace: Corvus.Ecma262.RegularExpressions
Assembly: Corvus.Regex.Ecma262.dll
Source: Regex.cs

An ECMA-262 regular expression: the grammar and the answers of ECMAScript 2025, with the shape of System.Text.RegularExpressions.Regex. It matches UTF-16 text (String and ReadOnlySpan of Char) and UTF-8 text (ReadOnlySpan of Byte) natively, with no transcoding either way. An instance is immutable and safe to share between threads.

public sealed class Regex

Inheritance

Object → Regex

Constructors

Constructor Description
Regex(...) Initializes a new instance of the Regex class with no flags and the...

Properties

Property Type Description
DefaultMatchTimeout static TimeSpan Gets the match timeout of every instance made without one of its own: the REGEX_DEFAULT_MATCH_TIMEOUT data of the AppContext, which `...
Edition RegexEdition Gets the edition of ECMA-262 whose grammar the pattern was read with.
Flags RegexFlags Gets the flags.
FlagsString string Gets the flags as the string RegExp.prototype.flags gives: the letters in alphabetical order.
GroupCount int Gets the number of capturing groups of the pattern.
IsUnicode bool Gets a value indicating whether the pattern is matched by code point (the u or v flag) rather than by UTF-16 code unit.
MatchTimeout TimeSpan Gets the time a match may take before it is abandoned, or InfiniteMatchTimeout.
Pattern string Gets the pattern.
UnicodeVersion RegexUnicodeVersion Gets the version of Unicode whose character database the pattern was read with, resolved: never Default or...

Methods

Method Description
Count The number of matches in a text.
CountUnchecked(ReadOnlySpan<byte>) The number of matches in a UTF-8 text, allocating nothing.
EnumerateMatches Enumerates the matches in a UTF-16 text, allocating nothing.
EnumerateMatchesUnchecked Enumerates the matches in a UTF-8 text, allocating nothing.
EnumerateSplits Enumerates the pieces a UTF-16 text splits into at the matches, as positions, allocating nothing. The pieces are those of Split without...
EnumerateSplitsUnchecked Enumerates the pieces a UTF-8 text splits into at the matches, as positions, allocating nothing (see EnumerateSplits).
Escape(string) static Escapes a text so that it matches itself as a pattern, as RegExp.escape of ECMAScript 2025 does: a first character that is a letter or a digit, the syntax characters, the other punctuators, white...
GetGroupNames() The names of the named groups, in the order the pattern first uses them.
GroupNameFromNumber(int) The name of a group, or its number as text when it has none.
GroupNumberFromName(string) The number of the first group of a name, or -1 when the pattern has no group of that name.
IsMatch static Whether a pattern matches anywhere in a text, with no flags.
IsMatchUnchecked Whether the pattern matches anywhere in a UTF-8 text.
IsValid static Whether a pattern is a valid ECMA-262 pattern in the grammar of the flags.
IsValidPattern static Whether a pattern is valid in the grammar of ECMA-262 for the flags, decided with nothing allocated. It asks the grammar only: unlike...
Match static The first match of a pattern in a text, with no flags.
Matches static Every match of a pattern in a text.
ParseFlags(string) static Reads the flags as the string new RegExp(pattern, flags) takes.
Replace static Replaces the matches of a pattern in a text (every match with Global, the first otherwise).
ReplaceUnchecked Replaces the matches in a UTF-8 text and writes the result to a buffer writer, allocating nothing itself.
Split static Splits a text at the matches of a pattern, as String.prototype.split does.
ToString()
TryCreate static Creates a regular expression without throwing for a pattern that is not valid.
TryMatch Finds the first match in a UTF-16 text at or after a position and reports what each group captured, allocating nothing.
TryMatchUnchecked(ReadOnlySpan<byte>, int, Span<ValueCapture>, ValueMatch) Finds the first match in a UTF-8 text at or after a position and reports what each group captured, allocating nothing.
TryReplace Replaces the matches in a UTF-16 text (every match with Global, the first otherwise) and writes the result to a span, allocating n...
TryReplaceUnchecked(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, int) Replaces the matches in a UTF-8 text and writes the result to a span, allocating nothing (see TryReplace).
Unescape(string) static Reverses Escape: reads the escapes it writes, and a backslash before any other character as that character.

Fields

Field Type Description
InfiniteMatchTimeout static TimeSpan The match timeout that means no timeout.

Applies To

Product Versions
.NET 9, 10
.NET Standard 2.0, 2.1

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests.

Open an issue