Skip to main content

Module regex

Module regex 

Source
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 defaultTranslation
\d, \w, \b are ASCII-onlyUnicode-awareexplicit ASCII classes; \b as lookarounds
\s is a fixed set incl. U+FEFFUnicode White_Spacethe JS set, listed explicitly
. excludes \n \r U+2028 U+2029excludes \n onlyexplicit class
\– (escaped non-ASCII) is a literalerrorthe literal character
[, &&, --, ~~ in a class are literalsclass syntaxescaped
lookahead/lookbehind, backreferencesunsupported in regexfancy-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.
RegexMatch
The result of RegExp.prototype.exec: index is a byte offset into the searched text, and groups[i] is match[i] (None = undefined, a group that didn’t participate).

Functions§

translate
Translates JS regex source (non-unicode mode) to fancy-regex syntax.