Documentation
¶
Overview ¶
Package ecmaregex is regular expressions with the grammar and the answers of ECMA-262, the RegExp of JavaScript.
re := ecmaregex.MustCompile(`(?<year>\d{4})-(?<month>\d{2})`, "u")
re.MatchString("on 2026-10-25") // true
re.ReplaceAllString("2026-10-25", "$<month>/$<year>") // "10/2026-25"
A pattern is read as new RegExp(pattern, flags) reads it: with the u or v flag by the grammar of the u flag and matched by code point; with neither by the Annex B grammar and matched by UTF-16 code unit, as ECMA-262 matches a pattern with no u flag. The i, m and s flags are the modifiers in force at the start of the pattern, which a modifier group of ECMAScript 2025 ((?i:...), (?-m:...)) changes for part of it.
Text is UTF-8 (a string) or UTF-16 ([]uint16, which may hold lone surrogates, with the methods ending in UTF16). Positions are in the units of the text: bytes of UTF-8, or UTF-16 code units. Nothing is transcoded. Text that is not UTF-8 is refused: a Try method returns an error, and any other panics.
A pattern has a match timeout, as a Regex of .NET's System.Text.RegularExpressions has: the time each match may take, which brings a cap on the memory of its backtracking with it. A pattern compiled with one should be matched with the Try methods, which return ErrTime or ErrMemory when a match reaches a limit; the others panic then. A pattern without a timeout never reaches a limit. SetDefaultMatchTimeout gives every pattern compiled after it one.
The engine is a port of the Rust crate corvus-regex-ecma262, file for file (parse.go is parse.rs, compile.go is compile.rs, and so on), held to the same ECMA-262 test data.
Index ¶
- Constants
- Variables
- func DefaultMatchTimeout() time.Duration
- func IsValid(pattern, flags string) bool
- func SetDefaultMatchTimeout(d time.Duration)
- type Captures
- type Error
- type ErrorKind
- type Flags
- type Limit
- type Matches
- func (m *Matches) Count() int
- func (m *Matches) Next(c *Captures) bool
- func (m *Matches) NextIndex() (int, int, bool)
- func (m *Matches) ResetString(s string) *Matches
- func (m *Matches) ResetText(t Text) *Matches
- func (m *Matches) ResetUTF16(u []uint16) *Matches
- func (m *Matches) TryCount() (int, error)
- func (m *Matches) TryNext(c *Captures) (bool, error)
- func (m *Matches) TryNextIndex() (int, int, bool, error)
- type Piece
- type Piece16
- type Regexp
- func (re *Regexp) CapturesString(c *Captures, s string, start int) bool
- func (re *Regexp) CapturesText(c *Captures, t Text, start int) bool
- func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
- func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
- func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int
- func (re *Regexp) FindIndex(b []byte) []int
- func (re *Regexp) FindStringIndex(s string) []int
- func (re *Regexp) FindStringSubmatchIndex(s string) []int
- func (re *Regexp) FindSubmatchIndex(b []byte) []int
- func (re *Regexp) Flags() Flags
- func (re *Regexp) Match(b []byte) bool
- func (re *Regexp) MatchString(s string) bool
- func (re *Regexp) MatchText(t Text) bool
- func (re *Regexp) MatchTimeout() time.Duration
- func (re *Regexp) MatchUTF16(u []uint16) bool
- func (re *Regexp) NewCaptures() *Captures
- func (re *Regexp) NewMatches() *Matches
- func (re *Regexp) NumSubexp() int
- func (re *Regexp) Pattern() string
- func (re *Regexp) Replace(src, template []byte) []byte
- func (re *Regexp) ReplaceAll(src, template []byte) []byte
- func (re *Regexp) ReplaceAllString(src, template string) string
- func (re *Regexp) ReplaceString(src, template string) string
- func (re *Regexp) Split(s string, limit int) []Piece
- func (re *Regexp) String() string
- func (re *Regexp) SubexpIndex(name string) []int
- func (re *Regexp) SubexpNames() []string
- func (re *Regexp) TryCapturesString(c *Captures, s string, start int) (bool, error)
- func (re *Regexp) TryCapturesText(c *Captures, t Text, start int) (bool, error)
- func (re *Regexp) TryCapturesUTF16(c *Captures, u []uint16, start int) (bool, error)
- func (re *Regexp) TryFindAllIndex(b []byte, n int) ([][]int, error)
- func (re *Regexp) TryFindAllStringIndex(s string, n int) ([][]int, error)
- func (re *Regexp) TryFindAllStringSubmatchIndex(s string, n int) ([][]int, error)
- func (re *Regexp) TryFindAllSubmatchIndex(b []byte, n int) ([][]int, error)
- func (re *Regexp) TryFindAllTextIndex(t Text, n int) ([][]int, error)
- func (re *Regexp) TryFindAllUTF16Index(u []uint16, n int) ([][]int, error)
- func (re *Regexp) TryFindAllUTF16SubmatchIndex(u []uint16, n int) ([][]int, error)
- func (re *Regexp) TryFindIndex(b []byte) ([]int, error)
- func (re *Regexp) TryFindStringIndex(s string) ([]int, error)
- func (re *Regexp) TryFindStringIndexAt(s string, start int) ([]int, error)
- func (re *Regexp) TryFindStringSubmatchIndex(s string) ([]int, error)
- func (re *Regexp) TryFindStringSubmatchIndexAt(s string, start int) ([]int, error)
- func (re *Regexp) TryFindSubmatchIndex(b []byte) ([]int, error)
- func (re *Regexp) TryFindTextIndexAt(t Text, start int) ([]int, error)
- func (re *Regexp) TryFindUTF16Index(u []uint16, start int) ([]int, error)
- func (re *Regexp) TryFindUTF16SubmatchIndex(u []uint16, start int) ([]int, error)
- func (re *Regexp) TryMatch(b []byte) (bool, error)
- func (re *Regexp) TryMatchString(s string) (bool, error)
- func (re *Regexp) TryMatchStringAt(s string, start int) (bool, error)
- func (re *Regexp) TryMatchText(t Text) (bool, error)
- func (re *Regexp) TryMatchUTF16(u []uint16) (bool, error)
- func (re *Regexp) TryMatchUTF16At(u []uint16, start int) (bool, error)
- func (re *Regexp) TryReplace(src, template []byte) ([]byte, error)
- func (re *Regexp) TryReplaceAll(src, template []byte) ([]byte, error)
- func (re *Regexp) TryReplaceAllString(src, template string) (string, error)
- func (re *Regexp) TryReplaceAllText(src Text, template string) (string, error)
- func (re *Regexp) TryReplaceAllUTF16(src, template []uint16) ([]uint16, error)
- func (re *Regexp) TryReplaceString(src, template string) (string, error)
- func (re *Regexp) TryReplaceText(src Text, template string) (string, error)
- func (re *Regexp) TryReplaceUTF16(src, template []uint16) ([]uint16, error)
- func (re *Regexp) TrySplit(s string, limit int) ([]Piece, error)
- func (re *Regexp) TrySplitText(t Text, limit int) ([]Piece, error)
- func (re *Regexp) TrySplitUTF16(u []uint16, limit int) ([]Piece16, error)
- type Text
- type TooLargeError
Constants ¶
const ( // MaxPatternLength is the longest pattern that is read, in bytes of UTF-8. MaxPatternLength = 1 << 20 // MaxNesting caps the depth of groups, which bounds the recursion of the parser and of the compilers. MaxNesting = 1000 // MaxSize is the largest size a pattern may have, in the bytes the parser counts for it: nodeSize for each node of // its syntax tree, and rangeSize for each range of a class as its members are read, for each range a case // insensitive group or a negated property adds, for each alternative that encloses a named group and for each // group that shares its name. The tree, the program compiled from it and the time to make both are proportional // to that size, so it bounds them all: a pattern at the limit takes a few milliseconds and a few megabytes. MaxSize = 1 << 20 )
The limits on the size of one pattern. A pattern may come from outside, so reading and compiling one must take bounded time and memory whatever it holds. A pattern over a limit is refused with a *TooLargeError, which is not the error of a pattern that is not valid.
const MaxMemoWords = 1 << 20
MaxMemoWords is the largest memory of failed places, in words of 64 bits: 8 MB. A match that would need more goes without it.
const MaxStack = 1 << 20
MaxStack is the number of entries the backtracking stack of a match with a limit may hold. A match that needs more is abandoned. A pattern that keeps a choice for every character of its text needs one entry or a few for each, so this is room for a text of some hundred thousand characters, and a pattern that pushes without consuming, such as (?:a?){2147483647}, reaches it in a few milliseconds. It is looked at with the clock, so the stack can go past it by checkEvery entries.
const Version = "0.1.0"
Version is the version of the module. A release writes its tag's version here; in the repository it is 0.0.0.
Variables ¶
var ( // ErrTime says the match ran for longer than its limit. ErrTime = errors.New("ecmaregex: the match ran for longer than its limit") // ErrMemory says the match needed more backtracking memory than its limit: its stack reached MaxStack entries. ErrMemory = errors.New("ecmaregex: the match needed more backtracking memory than its limit") )
ErrTime and ErrMemory say why a match was abandoned. Neither is an answer.
Functions ¶
func DefaultMatchTimeout ¶
DefaultMatchTimeout returns the match timeout a pattern compiled now without one of its own takes, or 0 for none.
func SetDefaultMatchTimeout ¶
SetDefaultMatchTimeout gives every pattern compiled after it, without a timeout of its own, the match timeout d. Zero is no timeout.
Types ¶
type Captures ¶
type Captures struct {
// contains filtered or unexported fields
}
Captures holds the start and end of a match and of each group of a pattern. A match into one allocates nothing, so a program that matches many times makes one with NewCaptures and passes it to each match. A Captures is not safe for concurrent use.
func (*Captures) Index ¶
Index returns the start and end of each group, -1 for a group that did not take part, as FindStringSubmatchIndex does, or nil when the last match found none. It is the Captures' own memory, which the next match overwrites.
type Error ¶
type Error struct {
Kind ErrorKind
// TooLarge says which limit a pattern of kind ErrTooLarge is over.
TooLarge *TooLargeError
// contains filtered or unexported fields
}
Error is why a pattern was not compiled, or why a text was refused.
type ErrorKind ¶
type ErrorKind uint8
ErrorKind says why a pattern was not compiled.
const ( // ErrInvalid says the pattern is not valid under the grammar its flags choose. ErrInvalid ErrorKind = iota // ErrFlags says the flags are not valid. ErrFlags // ErrTooLarge says the pattern is over a limit of size (see TooLargeError), which says nothing of whether it is // valid. ErrTooLarge // ErrEncoding says the pattern or a text is not UTF-8. ErrEncoding )
type Flags ¶
type Flags struct {
HasIndices bool // d
Global bool // g
IgnoreCase bool // i
Multiline bool // m
DotAll bool // s
Unicode bool // u
UnicodeSets bool // v
Sticky bool // y
}
Flags are the flags of a pattern, as new RegExp(pattern, flags) reads them.
func ParseFlags ¶
ParseFlags reads flags written as new RegExp reads them, such as "giu". A flag that is not one of dgimsuvy, a flag given twice, or u with v, is an error of kind ErrFlags.
type Matches ¶
type Matches struct {
// contains filtered or unexported fields
}
Matches is a global search that can be run again over another text, so that counting or reading the matches of many texts allocates nothing once the working memory has grown. After an empty match the search moves on one character, as matchAll does. A Matches is not safe for concurrent use.
m := re.NewMatches()
c := re.NewCaptures()
for m.ResetString(s); m.Next(c); {
fmt.Println(c.Start(0), c.End(0))
}
func (*Matches) Count ¶
Count is TryCount, which panics when a match reaches a limit or the text is not UTF-8.
func (*Matches) Next ¶
Next is TryNext, which panics when a match reaches a limit or the text is not UTF-8.
func (*Matches) NextIndex ¶
NextIndex is TryNextIndex, which panics when a match reaches a limit or the text is not UTF-8.
func (*Matches) ResetString ¶
ResetString starts the search over a string, which is checked to be UTF-8 when the search starts. A string that is not UTF-8 makes TryNext return an error of kind ErrEncoding, and Next panic.
func (*Matches) ResetUTF16 ¶
ResetUTF16 starts the search over UTF-16 text.
type Piece ¶
Piece is one item of a split: a piece of the text, or what a group that did not take part leaves, which ECMA-262 gives as undefined.
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
Regexp is a compiled pattern. It is safe for concurrent use.
func Compile ¶
Compile compiles a pattern with flags written as new RegExp reads them, such as "giu", and the default match timeout.
func CompileFlags ¶
CompileFlags compiles a pattern with the flags and the match timeout (0 for none).
func MustCompile ¶
MustCompile is Compile, which panics when the pattern is not compiled.
func (*Regexp) CapturesString ¶
CapturesString is TryCapturesString, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) CapturesText ¶
CapturesText is TryCapturesText, which panics when a match reaches a limit.
func (*Regexp) FindAllIndex ¶
FindAllIndex is TryFindAllIndex, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) FindAllStringIndex ¶
FindAllStringIndex is TryFindAllStringIndex, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) FindAllStringSubmatchIndex ¶
FindAllStringSubmatchIndex is TryFindAllStringSubmatchIndex, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) FindAllSubmatchIndex ¶
FindAllSubmatchIndex is TryFindAllSubmatchIndex, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) FindIndex ¶
FindIndex is TryFindIndex, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) FindStringIndex ¶
FindStringIndex is TryFindStringIndex, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) FindStringSubmatchIndex ¶
FindStringSubmatchIndex is TryFindStringSubmatchIndex, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) FindSubmatchIndex ¶
FindSubmatchIndex is TryFindSubmatchIndex, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) Match ¶
Match is TryMatch, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) MatchString ¶
MatchString is TryMatchString, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) MatchTimeout ¶
MatchTimeout returns the time each match may take, or 0 for no limit.
func (*Regexp) MatchUTF16 ¶
MatchUTF16 is TryMatchUTF16, which panics when a match reaches a limit.
func (*Regexp) NewCaptures ¶
NewCaptures returns a Captures for the pattern's matches.
func (*Regexp) NewMatches ¶
NewMatches returns a global search of the pattern, over no text until it is reset.
func (*Regexp) Replace ¶
Replace is TryReplace, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) ReplaceAll ¶
ReplaceAll is TryReplaceAll, which panics when a match reaches a limit or the bytes are not UTF-8.
func (*Regexp) ReplaceAllString ¶
ReplaceAllString is TryReplaceAllString, which panics when a match reaches a limit or a text is not UTF-8.
func (*Regexp) ReplaceString ¶
ReplaceString is TryReplaceString, which panics when a match reaches a limit or a text is not UTF-8.
func (*Regexp) Split ¶
Split is TrySplit, which panics when a match reaches a limit or the text is not UTF-8.
func (*Regexp) SubexpIndex ¶
SubexpIndex returns the groups with the name, which is more than one where groups in separate alternatives share it, or nil.
func (*Regexp) SubexpNames ¶
SubexpNames returns the name of each group, the whole match first (always ""). A group with no name has "". Two groups in separate alternatives can share a name.
func (*Regexp) TryCapturesString ¶
TryCapturesString matches the text from the byte offset start (at it alone with the y flag) into c, and reports whether it found a match.
func (*Regexp) TryCapturesText ¶
TryCapturesText matches the text from the byte offset start into c, and reports whether it found a match.
func (*Regexp) TryCapturesUTF16 ¶
TryCapturesUTF16 matches the text from the code unit start (at it alone with the y flag) into c, and reports whether it found a match.
func (*Regexp) TryFindAllIndex ¶
TryFindAllIndex returns the start and end of each match of a global search, at most n (all when n is below 0).
func (*Regexp) TryFindAllStringIndex ¶
TryFindAllStringIndex returns the start and end of each match of a global search, at most n of them (all when n is below 0). After an empty match the search moves on one character, as matchAll does.
func (*Regexp) TryFindAllStringSubmatchIndex ¶
TryFindAllStringSubmatchIndex is TryFindAllStringIndex with the start and end of each group of each match.
func (*Regexp) TryFindAllSubmatchIndex ¶
TryFindAllSubmatchIndex is TryFindAllIndex with the start and end of each group of each match.
func (*Regexp) TryFindAllTextIndex ¶
TryFindAllTextIndex returns the start and end of each match of a global search, at most n (all when n is below 0).
func (*Regexp) TryFindAllUTF16Index ¶
TryFindAllUTF16Index returns the start and end of each match of a global search, at most n of them (all when n is below 0).
func (*Regexp) TryFindAllUTF16SubmatchIndex ¶
TryFindAllUTF16SubmatchIndex is TryFindAllUTF16Index with the start and end of each group of each match.
func (*Regexp) TryFindIndex ¶
TryFindIndex returns the start and end of the first match, or nil.
func (*Regexp) TryFindStringIndex ¶
TryFindStringIndex returns the start and end of the first match, or nil.
func (*Regexp) TryFindStringIndexAt ¶
TryFindStringIndexAt returns the start and end of the first match at or after the byte offset start (at it alone with the y flag), or nil.
func (*Regexp) TryFindStringSubmatchIndex ¶
TryFindStringSubmatchIndex returns the start and end of the first match and of each group, -1 for a group that did not take part, or nil.
func (*Regexp) TryFindStringSubmatchIndexAt ¶
TryFindStringSubmatchIndexAt is TryFindStringSubmatchIndex from the byte offset start.
func (*Regexp) TryFindSubmatchIndex ¶
TryFindSubmatchIndex returns the start and end of the first match and of each group, -1 for a group that did not take part, or nil.
func (*Regexp) TryFindTextIndexAt ¶
TryFindTextIndexAt returns the start and end of the first match at or after the byte offset start, or nil.
func (*Regexp) TryFindUTF16Index ¶
TryFindUTF16Index returns the start and end of the first match at or after the code unit start, or nil.
func (*Regexp) TryFindUTF16SubmatchIndex ¶
TryFindUTF16SubmatchIndex returns the start and end of the first match at or after the code unit start and of each group, -1 for a group that did not take part, or nil.
func (*Regexp) TryMatchString ¶
TryMatchString reports whether the pattern matches the text.
func (*Regexp) TryMatchStringAt ¶
TryMatchStringAt reports whether the pattern matches the text from the byte offset start.
func (*Regexp) TryMatchText ¶
TryMatchText reports whether the pattern matches the text.
func (*Regexp) TryMatchUTF16 ¶
TryMatchUTF16 reports whether the pattern matches the text.
func (*Regexp) TryMatchUTF16At ¶
TryMatchUTF16At reports whether the pattern matches the text from the code unit start.
func (*Regexp) TryReplace ¶
TryReplace replaces the first match, or every match with the g flag, as String.prototype.replace does. The result is a new slice, or src itself when nothing matched.
func (*Regexp) TryReplaceAll ¶
TryReplaceAll replaces every match, as String.prototype.replaceAll does.
func (*Regexp) TryReplaceAllString ¶
TryReplaceAllString replaces every match, as String.prototype.replaceAll does.
func (*Regexp) TryReplaceAllText ¶
TryReplaceAllText replaces every match, as String.prototype.replaceAll does.
func (*Regexp) TryReplaceAllUTF16 ¶
TryReplaceAllUTF16 replaces every match, as String.prototype.replaceAll does.
func (*Regexp) TryReplaceString ¶
TryReplaceString replaces the first match, or every match with the g flag, as String.prototype.replace does, with the template's $ forms ($$, $&, $`, $', $n, $nn and $<name>) replaced.
func (*Regexp) TryReplaceText ¶
TryReplaceText replaces the first match, or every match with the g flag, as String.prototype.replace does.
func (*Regexp) TryReplaceUTF16 ¶
TryReplaceUTF16 replaces the first match, or every match with the g flag, as String.prototype.replace does.
func (*Regexp) TrySplit ¶
TrySplit splits the text around the matches, as String.prototype.split does: the groups of each match come between the pieces, and at most limit items (all when limit is below 0).
func (*Regexp) TrySplitText ¶
TrySplitText splits the text around the matches, as String.prototype.split does.
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text is UTF-8 text that has been checked once, so that the methods that take it check nothing. A program that matches one text many times makes a Text of it and pays for the check once.
func NewText ¶
NewText checks that the bytes are UTF-8 and returns them as a Text, which holds them without a copy: they must not be changed while it is in use. Bytes that are not UTF-8 are an error of kind ErrEncoding.
func NewTextString ¶
NewTextString checks that the string is UTF-8 and returns it as a Text.
type TooLargeError ¶
TooLargeError reports a pattern over one of the limits of size. Limit names the limit and Value is its value. The pattern was not read to its end, so the error says nothing of whether it is valid.
func (*TooLargeError) Error ¶
func (e *TooLargeError) Error() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
expand
command
Command expand writes out the templates of the matcher, one file for each encoding, as go generate runs it from the module's root:
|
Command expand writes out the templates of the matcher, one file for each encoding, as go generate runs it from the module's root: |
|
ucd
Package ucd holds the Unicode Character Database properties the engine reads, for one fixed version of Unicode (ucd.rs of the Rust crate).
|
Package ucd holds the Unicode Character Database properties the engine reads, for one fixed version of Unicode (ucd.rs of the Rust crate). |
|
tools
|
|
|
dataset
command
Command dataset runs the engine over the ECMA-262 regular expression test data (github.com/corvus-dotnet/ecma262-regex-test-data), as the runner of the Rust crate does (tools/src/bin/dataset.rs there), with the same judging rules.
|
Command dataset runs the engine over the ECMA-262 regular expression test data (github.com/corvus-dotnet/ecma262-regex-test-data), as the runner of the Rust crate does (tools/src/bin/dataset.rs there), with the same judging rules. |
|
stackprobe
command
Command stackprobe asks the engine about patterns built to overflow the stack: deep (groups, lookarounds, modifier groups, quantified groups and the classes of the v flag, each nested to the limit, one past it and far past it), wide (many groups, classes, alternatives, backreferences, set operations and string alternatives side by side) and long (the longest pattern allowed, and one past it), in every grammar, through every member over UTF-8 and UTF-16, as the Rust crate's stack-probe does.
|
Command stackprobe asks the engine about patterns built to overflow the stack: deep (groups, lookarounds, modifier groups, quantified groups and the classes of the v flag, each nested to the limit, one past it and far past it), wide (many groups, classes, alternatives, backreferences, set operations and string alternatives side by side) and long (the longest pattern allowed, and one past it), in every grammar, through every member over UTF-8 and UTF-16, as the Rust crate's stack-probe does. |