ecmaregex

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package ecmaregex runs ECMAScript regular expressions: the engine behind `new RegExp(source, "u")` and `new RegExp(source)` for patterns without the d, g, v and y flags, which is what packages/schema/src/shared.ts (sharedRegex) and packages/schema/src/formats/typebox/regex.ts construct. It parses a pattern with the grammar V8 uses, translates it once into github.com/dlclark/regexp2 syntax and matches with that engine over UTF-16 code units. The i, m and s flags exist only as the modifier groups (?i:), (?m:) and (?s:) that V8 13.6 (Node 24) reads.

No match timeout is set, as in V8: a pattern that backtracks catastrophically on a subject runs as long as it does there, so a pattern from an untrusted source can hold a goroutine for as long as it likes.

Differences from V8 (Node 24), each pinned by a test:

  • A backreference inside a case-insensitive modifier group compares with unicode.ToLower, not Canonicalize, so it differs on pairs such as ς and σ, ſ and s, İ and i (knownGaps in golden_test.go).
  • V8 answers differently, for reasons its source does not make plain, for a few patterns that combine \W or a class after a case-insensitive modifier group in plain mode, and a long alternation under the m and s modifiers (knownGaps).
  • Which strings V8 stores with one byte per unit is an implementation detail that a modifier group sees (see parser.oneByte); a string counts as one-byte here when every code unit is at most U+00FF.
  • The Unicode data comes from Go's unicode package (unicode.Version) except the properties named in property.go, which are generated from V8's own tables; a toolchain on an older Unicode version lags V8 by the characters assigned in between.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Valid

func Valid(source string) bool

Valid reports whether Compile(source) succeeds, which is what the "regex" format checks.

Types

type Regexp

type Regexp struct {
	// contains filtered or unexported fields
}

A Regexp is a compiled regular expression. It is safe for concurrent use by multiple goroutines: regexp2 hands each match its own runner.

func Compile

func Compile(source string) (*Regexp, error)

Compile is `new RegExp(source, "u")`: the pattern is a sequence of code points and the strict grammar applies. The error is the SyntaxError V8 throws.

func CompileAnnexB

func CompileAnnexB(source string) (*Regexp, error)

CompileAnnexB is `new RegExp(source)`: the pattern is a sequence of UTF-16 code units and the Annex B grammar applies, which allows a lone "{", "}" or "]", identity escapes such as \_ , legacy octal escapes, \c without a letter and quantified lookaheads.

func (*Regexp) MatchString

func (r *Regexp) MatchString(s string) bool

MatchString is RegExp.prototype.test for an expression without the g and y flags: it reports whether any part of s matches. Like V8, it runs over the UTF-16 code units of s, and s may hold lone surrogates as WTF-8.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL