UNPKG

@imhonglu/pattern-builder

Version:

Type-safe regular expression pattern builder for TypeScript with fluent API

121 lines (110 loc) 3.02 kB
/** The pre-defined alpha pattern. */ export declare const alpha: Characters; export declare class Characters extends PatternBuilder { negate(): this; clone(): Characters; } /** * Creates a pattern builder for the character set operator. * * @param patterns - The patterns to create the character set operator from. * @returns The pattern builder. * * @example pre-defined pattern with string * ```ts * characterSet(alpha, '#!'); * // => /[a-zA-Z#!]/ * ``` * * @example pre-defined pattern with pattern * ```ts * characterSet(digit, /[a-fA-F]/); * // => /[\da-fA-F]/ * ``` * * @example only string * ```ts * characterSet("a", "b", "c"); * // => /[abc]/ * ``` */ export declare function characterSet(...patterns: PatternInput[]): Characters; /** * Creates a pattern builder for the concat operator. * * @param patterns - The patterns to create the concat operator from. * @returns The pattern builder. * * @example only string * ```ts * concat("a", "b", "c"); * // => /abc/ * ``` * * @example pre-defined pattern with pattern * ```ts * concat(alpha, digit); * // => /[a-zA-Z][\d]/ * ``` * * @example pre-defined pattern with string * ```ts * concat(alpha, "0"); * // => /[a-zA-Z]0/ * ``` */ export declare function concat(...patterns: PatternInput[]): PatternBuilder; /** The pre-defined digit pattern. */ export declare const digit: Characters; /** The pre-defined hex digit pattern. */ export declare const hexDigit: Characters; /** * Creates a pattern builder for the one of operator. * * @param patterns - The patterns to create the one of operator from. * @returns The pattern builder. * * @example only string * ```ts * oneOf("a", "b", "c"); * // => /a|b|c/ * ``` * * @example pre-defined pattern with pattern * ```ts * oneOf(alpha, digit); * // => /[a-zA-Z]|[\d]/ * ``` * * @example pre-defined pattern with string * ```ts * oneOf(alpha, "0"); * // => /[a-zA-Z]|0/ * ``` */ export declare function oneOf(...patterns: PatternInput[]): PatternBuilder; export declare class PatternBuilder { source: string; constructor(source: string); anchor(position?: "start" | "end"): PatternBuilder; group(): this; nonCapturingGroup(): this; lookahead(): this; negateLookahead(): this; lookbehind(): this; negateLookbehind(): this; oneOrMore(): this; zeroOrMore(): this; exact(count: number): this; repeat(min: number, max?: number): this; optional(): this; clone(): PatternBuilder; toString(): string; toRegExp(...flags: PatternFlag[]): RegExp; } /** * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags | MDN} */ export declare type PatternFlag = "d" | "g" | "i" | "m" | "s" | "u" | "v" | "y"; export declare type PatternInput = string | PatternBuilder | RegExp; export { }