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&strcan 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), ornull. AdvanceslastIndexunderg/y.- regexp_
method - Dispatch a
RegExp.prototypemethod. - regexp_
property - A
RegExpown data property (source/flags/global/…/lastIndex), orNoneifnameis not one (so the caller tries methods). - regexp_
test re.test(s)— honoringg/ylastIndexadvancement, exactly likeexec.- str_
match str.match(re): withoutg, same asexec(array or null); withg, an array of every whole-match string (or null if none).- str_
match_ all str.matchAll(re): an iterator over every match array (requires thegflag 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).replis 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.14RegExp.prototype [@@split].