Skip to main content

Module regexp

Module regexp 

Source
Expand description

JavaScript RegExp on top of the fancy_regex crate.

fancy-regex wraps the linear Rust regex engine and layers a backtracking matcher on top, so it can express the JS constructs plain regex cannot: lookahead ((?=)/(?!)), lookbehind ((?<=)/(?<!)), and backreferences (\1, \k<name>). node-js therefore accepts a near-superset of the JS regex grammar; the small residue fancy-regex still cannot represent is documented in BUGS.md and rejected loudly at construction time (never a silently-wrong match).

What translate still has to do (fancy-regex/regex differ from JS here):

  • \uXXXX / \u{...} → \x{...} (regex spells fixed code points that way), with lone-surrogate escapes (\uD800..\uDFFF) mapped into a Plane-15 private-use block — surrogate code points are not valid Unicode scalar values, so \x{D800} will not compile; a valid UTF-8 &str can never contain a lone surrogate anyway, so those alternatives stay dead (correct for all valid input, e.g. encodeurl’s unmatched-surrogate scan).
  • \/ in a literal → a plain / (regex rejects the redundant escape).
  • \N / \k<name> → a conditional, so a reference to an unset group matches empty as in JS; the Annex B legacy escapes (\0, octal, \cX, identity \8/\k) become fixed code points; non-JS group syntax ((?i), (?P<n>, (?>) is rejected with node’s reason.

Everything else — including (?<name>...), (?=)/(?!), (?<=)/(?<!) and the (?ims-ims:...) modifier groups — passes through verbatim.

Flags: i/m/s map onto inline flags; g/y drive iteration and lastIndex here (fancy-regex has no global flag); u/d are accepted.

Structs§

Caps
The captures of one match, numbered and filtered as JS sees them.
CaptureMap
What a JS capture group number means to the engine, and which captures are STALE.
Engine
A compiled pattern together with its CaptureMap. Derefs to the engine for everything that does not look at captures.

Functions§

build_regexp
check_literal
The early errors of a regex LITERAL (13.2.7.3): its flags and its pattern grammar, in the wording V8 gives a literal. The flag message differs from the constructor’s, which names the argument.
incompatible_receiver
V8’s wording for a RegExp.prototype method whose receiver is not a RegExp.
is_regexp_method
regexp_exec
re.exec(s) — returns a match array ([full, ...captures] with .index, .input, .groups), or null. Advances lastIndex under g/y.
regexp_exec_generic
RegExpExec(R, S) (22.2.7.1). R’s exec is read with [[Get]]; one that is not the built-in — an own override, a subclass method, or the method of a plain object — is called and must return an object or null. Otherwise R has to be a RegExp, and the built-in matcher runs.
regexp_method
Dispatch a RegExp.prototype method.
regexp_property
A RegExp own data property (source/flags/global/…/lastIndex), or None if name is not one (so the caller tries methods).
regexp_test
re.test(s) — honoring g/y lastIndex advancement, exactly like exec.
regexp_test_generic
RegExp.prototype.test (22.2.6.16): RegExpExec and a non-null result. Generic over any object receiver; with the built-in exec it runs the matcher without building a match array.
regexp_to_string_generic
RegExp.prototype.toString (22.2.6.17): "/" + ToString(R.source) + "/" + ToString(R.flags), both read with [[Get]], for any object receiver.
str_match
str.match(re): without g, same as exec (array or null); with g, an array of every whole-match string (or null if none).
str_match_all
str.matchAll(re): an iterator over every match array (requires the g flag in Node, but we accept a non-global regex too and still iterate all matches).
str_replace_regex
str.replace(re, repl) / str.replaceAll(re, repl). repl is either a string (with $1/$&/$`/$'/$<name>/$$ patterns) or a function replacer.
str_search
str.search(re): char index of the first match, or -1.
str_split_regex
str.split(re[, limit]): split on regex matches; captured groups are spliced into the output (JS semantics). String.prototype.split(regexp[, limit]) — 22.2.6.14 RegExp.prototype [@@split].