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.

Functions§

build_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_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.
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].