/** * Advanced regex matching and parsing functionality. * This module provides enhanced regex matching capabilities with support for: * - Named groups and custom parsers * - Multiple match extraction * - Structured result parsing * - Flexible data extraction * * matchRaw => RawResult (custom class) * .raw : Raw (bare) * .parsedResult: ParsedResult (custom class) * .parsed: Parsed (bare) * .result: Result (custom class) * .extracted: Extracted (bare) * match => Result (custom class) * .extracted: Extracted (bare) * matchAndExtract => Extracted (bare) * * primarily internal use * parse(RawResult) => ParsedResult (custom class) * .parsed: Parsed (bare) * .result: Result (custom class) * .extracted: Extracted (bare) * extract(ParsedResult) => Result (custom class) * .extracted: Extracted (bare) */ import { RegExpFlags } from "./flags"; /** * Raw group value structure containing match information and metadata. * Represents a single match or group with position and content details. * This is the fundamental data structure returned by regex matching operations. * * @example * ```typescript * const rawMatch: RawGroupValue = { * name: "email", * raw: "john@example.com", * startIndex: 10, * endIndex: 25, * lastIndex: 25, * offset: 0, * pattern: /(?[^\s@]+@[^\s@]+)/, * groups: { * email: { raw: "john@example.com", startIndex: 10, endIndex: 25, ... } * } * }; * ``` */ export interface RawGroupValue { /** Optional name for the group (for named groups) */ name?: string; /** The RegExp pattern that was used for matching */ pattern: RegExp; /** Offset applied to indices (for nested matches) */ offset: number; /** Start index of the match in the original string */ startIndex: number; /** End index of the match in the original string */ endIndex: number; /** Last index after the match (for global matching) */ lastIndex: number; /** The raw matched string */ raw: string; /** Named groups captured within this match */ groups: Record; } /** * Parameters for controlling regex matching behavior. * Provides fine-grained control over matching options. */ interface RawMatchParams { /** Optional name for the match */ name?: string; /** Maximum number of matches to return (null = unlimited, undefined = auto) */ maxMatches?: number | null; /** Offset to add to all indices */ offset?: number; /** RegExp flags to apply */ flags?: RegExpFlags; /** Starting position for matching */ lastIndex?: number; /** Whether to cache the input string in the result objects */ cacheInput?: boolean; } /** * Union type for raw match results - single match, array of matches, or null. * This represents the bare data structure returned by regex matching operations. * * @example * ```typescript * // Single match * const singleMatch: Raw = { * name: "email", * raw: "john@example.com", * startIndex: 10, * endIndex: 25, * // ... other properties * }; * * // Multiple matches * const multipleMatches: Raw = [ * { name: "email", raw: "john@example.com", ... }, * { name: "email", raw: "jane@example.com", ... } * ]; * * // No match * const noMatch: Raw = null; * ``` */ export type Raw = RawGroupValue | null | RawGroupValue[]; /** * Class wrapper for raw match results that provides a simple interface. * Behaves exactly like the original RawResult but adds a .result getter. */ export declare class RawResult { private _raw; private _parsed?; input: string | undefined; constructor(raw: Raw, input?: string); get raw(): Raw; parse(unnest?: boolean): ParsedResult; get parsed(): ParsedResult; get parsedValue(): Parsed; get result(): Result; get extracted(): any; /** * Returns the underlying parsed result as a plain object. */ valueOf(): Raw; /** * Custom toString for console.log to display as Raw value */ toString(): string; } /** * Performs raw regex matching with detailed position and group information. * This is the core matching function that extracts all match data including * indices, named groups, and metadata without any parsing or transformation. * * @param input - The input string to search * @param pattern - The regex pattern to match against * @param params - Matching parameters and options * @returns MatchRawResult instance with detailed position information and .parse() method */ export declare function matchRaw(input: string, pattern: string | number | RegExp, { maxMatches, offset, flags, lastIndex, name, cacheInput }?: RawMatchParams): RawResult; /** * Enhanced group value with parsed content and additional metadata. * Extends RawGroupValue with parsed data and unnesting information. */ export interface GroupValue extends RawGroupValue { /** The parsed/transformed value of the matched content */ parsed: any; /** Named groups with their own parsed values */ groups: Record; /** Whether this group should be unnested during extraction */ unnest: boolean; } /** * Type for string parsers that can transform matched content. * Can be a function, string pattern, number, or RegExp for further matching. */ export type StringParser = ((raw: string) => any) | ((raw: string, opts: { offset?: number; }) => any) | string | RegExp; type GroupsFn = (g: Record) => Record; export type Parsers = { groups?: GroupsFn; } & { [P in Exclude]?: StringParser; }; /** * Parser that attempts JSON parsing with fallback to original string. * Tries to parse the string as JSON, returns original if parsing fails. */ export declare const tryjson: StringParser; export declare const noop: (s: string) => string; /** Default parser used when no specific parser is provided */ export declare const defaultStringParser: (s: string) => string; /** * Creates a new RegExp with custom parsers for processing matched groups. * Merges new parsers with existing ones, with new parsers taking precedence. * Gets bound to RegExp.withParsers. * **NOTE**: only has an effect if `match(input, pattern)` or `matchAndExtract(input, pattern)` is called * * @param rx - The RegExp to add parsers to * @param parsers - Object mapping group names to parser functions * @returns A new RegExp with the specified parsers attached * * @example * // simple case: perform some conversion, define key: (s: string) => any * const regex = re`name: (?\w+), (?.*?), (?.*?), (?.*)`.withParsers({ * name: (s) => s.toUpperCase(), * _x: null, // use _${key}: null to ignore key * _next: (s) => { * // use _${key}: func to define an nunested parser, e.g. put info key at top level * return {"info": s} * }, * another: /(?\w*): (?\d*)/, * groups: (o) => { * return { * title: o.name * } * } * }); */ export declare function withParsers(rx: RegExp, parsers?: Parsers): RegExp; /** Union type for parsed match results - single group, array of groups, or null */ export type Parsed = GroupValue | GroupValue[] | null; /** * Class wrapper for parsed match results that provides a simple interface. * Behaves exactly like the original Result but adds lazy extraction. */ export declare class ParsedResult { private _parsed; private _result?; [k: string | number]: any; input: string | undefined; constructor(parsed: Parsed, input?: string); get parsed(): Parsed; /** * Gets the extracted data, extracting if not already cached. * @returns Extracted data in various structured formats */ get result(): Result; get extracted(): Extracted; /** * Returns the underlying parsed result as a plain object. */ valueOf(): Parsed; /** * Custom toString for console.log to display as Parsed value */ toString(): string; } export declare function parseValue(raw: any, parsers: Record, key: string, offset?: number): any; /** * Parses raw match results by applying custom parsers to matched content. * This function transforms raw match data into structured, parsed results * using the parsers attached to the RegExp pattern. * * @param raw - The raw match result to parse * @param unnest - Whether to unnest the result during extraction * @returns ParseResult instance with transformed content and groups and .extract() method */ export declare function parse(raw: Raw, unnest?: boolean, input?: string): ParsedResult; export type MatchOpts = RawMatchParams | undefined; /** * Performs regex matching with automatic parsing of matched content. * This is the main matching function that combines raw matching with parsing. * * @param input - The input string to search * @param pattern - The regex pattern to match against * @param params - Matching parameters and options * @returns ParseResult instance with transformed content and .extract() method * * @example * const result = match("name: John", /name: (?\w+)/); * // Returns ParseResult with name group processed and .extract() method */ export declare function match(input: string, pattern: string | number | RegExp, { offset, flags, maxMatches, cacheInput }?: MatchOpts): Result; /** Union type for extracted data - various formats of structured data */ export type Extracted = any | Record | Record[] | null | any[]; /** * Class wrapper for parsed match results that provides a simple interface. * Behaves exactly like the original Result but adds lazy extraction. */ export declare class Result { private _parsedResult; private _extracted; [k: string | number]: any; constructor(parsedResult: ParsedResult, extracted: Extracted); get input(): string | undefined; get parsedResult(): ParsedResult; get parsed(): Parsed; get groups(): Record; get extracted(): any; /** * Returns the underlying parsed result as a plain object. */ valueOf(): Extracted; /** * Custom toString for console.log to display as Extracted value */ toString(): string; } /** * Extracts structured data from parsed match results. * This function intelligently extracts and organizes data from match results, * handling various data structures and providing flexible output formats. * * @param v - The parsed match result to extract data from * @param k - Optional key name for the extracted data * @param d - Destination object to accumulate extracted data * @param flat - Whether to flatten nested structures * @returns Extracted data in various structured formats * * @example * const result = match("name: John, age: 25", /name: (?\w+), age: (?\d+)/); * const data = extract(result); // { name: "John", age: 25 } */ export declare function extract(parsedResult: ParsedResult, k?: string, d?: Record, flat?: boolean): Result; /** * Convenience function that combines matching and extraction in one step. * Performs regex matching and immediately extracts structured data from the results. * * @param input - The input string to search * @param pattern - The regex pattern to match against * @param opts - Matching parameters and options * @returns Extracted structured data from the match results * * @example * const data = matchAndExtract("name: John, age: 25", /name: (?\w+), age: (?\d+)/); * // Returns { name: "John", age: 25 } */ export declare function matchAndExtract(input: string, pattern: string | number | RegExp, opts?: MatchOpts): Extracted; export {}; //# sourceMappingURL=match.d.ts.map