package quickjs
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=42397865f43779613b04bb2f3532b060c6dcca2d41aa1337b31124fca21629a4
sha512=52bf83c9e3a9e37764b514e34e1acbcdabef9ccac9cefa510e4683b693328ddde42ff59e8475e19c9f1a90caee8b790f2438f5808a11eca7700412b55f41a30e
doc/quickjs/Quickjs/RegExp/index.html
Module Quickjs.RegExpSource
JavaScript RegExp built-in object, backed by QuickJS's libregexp.
Index units
Strings are UTF-8 encoded OCaml strings, but every index exposed by this module (index, last_index, set_last_index) is a UTF-16 code unit offset, exactly like JavaScript's RegExp. This keeps results consistent with Quickjs.String, whose indices are UTF-16 as well.
Lifetime and state
A compiled regexp owns C-allocated bytecode which is released automatically when the value is garbage collected. Like in JavaScript, regexps compiled with the global (g) or sticky (y) flag carry mutable matching state (last_index); a t value is therefore not safe to share between threads without synchronization.
The RegExp object
type match_indices = {ranges : (int * int) option array;(*Entry
*)gis the(start, end_)range of capture groupgin UTF-16 code units, withend_exclusive (the same convention as JavaScript'smatch.indices:startis the index of the first code unit of the capture andend_the index after the last one). Entry 0 is the full match. A group that did not participate isNone.groups : (string * (int * int) option) list;(*Ranges of named capture groups in source order, like JavaScript's
*)match.indices.groups, withNonefor groups that did not participate.
}Match positions, equivalent to JavaScript's match.indices (the RegExp Match Indices proposal, ES2022).
type match_result = {captures : string option array;(*Entry 0 is the full match; entries 1..n are capture groups. A group that did not participate in the match is
*)None(JavaScript'sundefined), which is distinct from a group that matched the empty string (Some "").index : int;(*UTF-16 index of the match start in
*)input.input : string;(*The input string that was matched against.
*)groups : (string * string option) list;(*Named capture groups in source order, with
*)Nonefor groups that did not participate.indices : match_indices option;(*Capture group positions.
*)Someiff the regexp was compiled with thedflag, like JavaScript'shasIndices.
}The result of a successful match.
An immutable input converted to the representation expected by libregexp. Preparing once and reusing this value avoids copying and converting the complete input for every match in a global iteration.
type source_range = {utf16 : int * int;(*Match range in UTF-16 code units, with the end exclusive.
*)bytes : (int * int) option;(*Exact range in the original UTF-8 string when both UTF-16 boundaries align with source character boundaries.
*)Nonewhen the range splits a surrogate pair or the source contained malformed UTF-8.
}A match against a prepared_input, including the full match range in both JavaScript index units and, when representable, source byte offsets.
type compile_error = [ | `Unexpected_end| `Malformed_unicode_char| `Invalid_escape_sequence| `Nothing_to_repeat| `Stack_overflow| `Invalid_flags of string| `Unknown of string
]Possible errors when compiling a RegExp pattern. `Invalid_flags is returned for unknown flags, duplicated flags, or combining u with v. `Stack_overflow is returned for patterns nested too deeply to compile.
Convert a compile error to a human-readable string
compile ~flags source compiles source with the given JavaScript flags (any of "dgimsuvy", each at most once; u and v are mutually exclusive).
Without the u/v flag the pattern is matched as UTF-16 code units (astral code points behave as surrogate pairs), mirroring JavaScript's non-unicode regexp semantics.
Returns the UTF-16 index where the next match will start its search. Only meaningful for regexps compiled with the global (g) or sticky (y) flag; it stays 0 otherwise, like in JavaScript.
Returns the enabled flags in canonical order ("dgimsuvy" subset), exactly as passed to compile.
prepare_input input converts input once for reuse by exec_prepared. The prepared value owns its matching buffer and keeps the original string alive.
prepared_byte_range input ~start ~end_ returns the exact UTF-8 byte range corresponding to UTF-16 offsets start (inclusive) and end_ (exclusive) when it can be sliced directly from the original string.
prepared_substring input ~start ~end_ extracts a UTF-16 range from the prepared input. A boundary that splits a surrogate pair is represented by U+FFFD because OCaml strings cannot encode unpaired surrogates.
prepared_advance_index input ~unicode index implements JavaScript's AdvanceStringIndex operation without rescanning the source. In Unicode mode, an index at the start of a surrogate pair advances by two UTF-16 code units; every other index advances by one.
exec_prepared regexp input has the same matching and last_index semantics as exec, but reuses input's matching buffer and UTF-16 map.
exec regexp input executes a search and returns the first match, or None when there is no match.
For global/sticky regexps the search starts at last_index, and last_index is updated to the end of the match (or reset to 0 on no match), enabling JavaScript-style match iteration.
timeout_ms bounds the execution time of the underlying engine; when exhausted, Timeout is raised. Without it, pathological patterns can backtrack for a very long time.
test regexp input is exec regexp input <> None. Like in JavaScript, it advances last_index on global/sticky regexps.
group name m returns the value of named capture group name, or None when the group does not exist or did not participate in the match.
group_indices name m returns the UTF-16 (start, end_) range of named capture group name (end_ exclusive), or None when the regexp was compiled without the d flag, the group does not exist, or it did not participate in the match. Equivalent to JavaScript's match.indices.groups[name].