Expand description
JavaScript regular expressions on top of fancy-regex.
Port patterns verbatim from the TypeScript source (the text between /.../ or inside
new RegExp("...") after JS string unescaping) and pass the JS flags. JsRegex translates
the parts where JS and Rust differ:
JS (no u flag) | Rust default | Translation |
|---|---|---|
\d, \w, \b are ASCII-only | Unicode-aware | explicit ASCII classes; \b as lookarounds |
\s is a fixed set incl. U+FEFF | Unicode White_Space | the JS set, listed explicitly |
. excludes \n \r U+2028 U+2029 | excludes \n only | explicit class |
\– (escaped non-ASCII) is a literal | error | the literal character |
[, &&, --, ~~ in a class are literals | class syntax | escaped |
| lookahead/lookbehind, backreferences | unsupported in regex | fancy-regex handles them |
| i compares Canonicalize(ch): the single-unit uppercase, never non-ASCII → ASCII, BMP only | Unicode simple case folding (U+017F LONG S ~ s, U+212A KELVIN SIGN ~ k, …) | no (?i): each literal becomes its JS-equivalence set, each class is closed under it |
With i, backreferences are rejected (JS compares them canonicalized; no ported pattern uses one).
Unicode mode (u), as far as ported patterns need it ([^\p{L}\p{N}\p{M}_] in Spanish):
\p{..}/\P{..} are Unicode property classes and \u{..} a code point (both passed to Rust),
and iu compares by Unicode simple case folding (ECMA-262 Canonicalize with rer.[[Unicode]]),
which is exactly Rust’s (?i). That includes \w/\b matching U+017F and U+212A, as JS iu
does. v (unicode sets) is not supported.
Structs§
- JsRegex
- A compiled JS regular expression.
- Regex
Match - The result of
RegExp.prototype.exec:indexis a byte offset into the searched text, andgroups[i]ismatch[i](None=undefined, a group that didn’t participate).
Functions§
- translate
- Translates JS regex source (non-unicode mode) to
fancy-regexsyntax.