regexpjs

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package regexpjs is the JS→regex translation layer: it parses/validates ECMAScript regular expressions and retargets them onto dlclark/regexp2, with translate-time expansion of property escapes (UCD 17.0) and v-flag set notation, sticky/anchored program variants, and ported RegExp.$1-$9 statics. See PLAN.md Phase 4.3 / 8.

Package regexpjs is the JS→regex translation layer (PLAN.md Phase 4.3/8): it validates ECMAScript regular expressions and retargets them onto dlclark/regexp2 (which supports an ECMAScript mode directly). Flags g/y and lastIndex handling live at the JS level; i/m/s/u map to regexp2 options.

Position semantics: subjects are passed in — and all offsets reported back — as UTF-16 code units, the domain ECMAScript indexes strings in. A `u`/`v` pattern matches whole code points, so Exec recodes such a subject and maps the resulting offsets back (see Exec).

regexp2 is the reference matcher, but not always the one that runs: re2.go translates the patterns it can into RE2 syntax for Go's own regexp, which is several times faster, and works out a literal run that any match must contain so a long subject without one can be rejected without matching at all.

Index

Constants

This section is empty.

Variables

View Source
var VerifyFast bool

VerifyFast makes every fast-path match run the regexp2 matcher as well and panic if the two disagree. It is a test hook, not a knob: the cost is running both engines.

Functions

func UnicodeBinaryProperty

func UnicodeBinaryProperty(name string) *unicode.RangeTable

UnicodeBinaryProperty returns the code-point set of a Unicode binary property by its canonical name ("Cased", "Case_Ignorable", …), or nil if there is no such property. It exposes the generated tables to callers outside the regexp engine — String.prototype.toLowerCase needs Cased / Case_Ignorable to decide the Final_Sigma context.

Types

type Group

type Group struct {
	Index  int
	Length int
	Value  string
	Name   string
}

Group is one capture group in a match (Index is a rune offset; -1 = unmatched).

type Match

type Match struct {
	Index  int
	Groups []Group
}

Match is a successful regex match with its capture groups (group 0 is whole).

type Regexp

type Regexp struct {
	Source string
	Flags  string

	Global      bool
	IgnoreCase  bool
	Multiline   bool
	DotAll      bool
	Unicode     bool
	UnicodeSets bool
	Sticky      bool
	// contains filtered or unexported fields
}

Regexp is a compiled JS regular expression.

func Compile

func Compile(pattern, flags string) (*Regexp, error)

func (*Regexp) Exec

func (r *Regexp) Exec(input []rune, start int) (*Match, error)

Exec runs the regex against input — the subject string as UTF-16 code units, one rune per unit — starting at code-unit index start, and returns the first match (or nil if none). Sticky matches must begin exactly at start. All returned offsets are code-unit indices, matching ECMAScript string indexing.

func (*Regexp) ExecASCII

func (r *Regexp) ExecASCII(s string, start int) (m *Match, handled bool)

ExecASCII is Exec for a subject already known to be pure ASCII, run on the translated pattern. handled is false when the call is not one the fast path can take — no translation, or a search from a nonzero offset by a pattern that reads text to the left of it — and the caller must fall back to Exec.

func (*Regexp) GroupCount

func (r *Regexp) GroupCount() int

GroupCount returns the number of capture groups (excluding group 0).

func (*Regexp) HasFast

func (r *Regexp) HasFast() bool

HasFast reports whether the pattern has an RE2 translation to run at all, working it out on the first ask.

func (*Regexp) Test

func (r *Regexp) Test(input []rune, start int) (bool, error)

Test reports whether the regex matches anywhere at/after start.

Jump to

Keyboard shortcuts

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