UNPKG

regexp-coder

Version:

A Javascript/Typescript RegExp Coder

230 lines (229 loc) 7.37 kB
import { RegExpOptions } from './regExpOptions'; /** * RegExp Param */ export declare type RegExpParam = undefined | string | RegExp | RegExpCoder | RegExpOptions; /** * Regular Expression Coder * * @see [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) */ export declare class RegExpCoder { /** * Chars that need escaped */ private static escapedChars; /** * Chars that need escaped in a set expression. */ private static escapedSetChars; private variables; private result; private isMatchWhole; /** * Create an instance of RegExpCoder */ static new(): RegExpCoder; /** * Get the last options if there is * @param exp the expression */ private getParamOptions; /** * Apply options to a specific expression * @param exp the expression * @param options the options */ private applyOptions; /** * Get the source of an expression or a variable * @param exp the expression */ private getSource; /** * create an expression * @param exp the expression */ private build; /** * Encode a raw string to a regular expression string. * Following special characters will be encoded: `^\.()[]?+*|$` * * For set case, encoded characters: `^\-]` * Rules: * - escape `^` only when it is the first char * - escape `-` only when it is not the first char and not the last char * - escaped `\]` always * @param raw the raw string * @param forSet if for set `[]` operation */ static encodeRegExp(raw: string, forSet?: boolean): string; /** * `xy` * Define a variable * @param name the variable name * @param exp the variable expression */ define(name: string, ...exp: RegExpParam[]): RegExpCoder; /** * `(xy)` * Define a variable for group operation * @param name the variable name * @param exp the variable expression */ defineGroup(name: string, ...exp: RegExpParam[]): RegExpCoder; /** * `x|y` * Define a variable for or operation * @param name the variable name * @param exp the variable expression */ defineOr(name: string, ...exp: RegExpParam[]): RegExpCoder; /** * `[xy]` * Define a variable for set * @param name the variable name * @param exp the variable expression */ defineSet(name: string, ...exp: RegExpParam[]): RegExpCoder; /** * `[^xy]` * Define a variable for negated set * @param name the variable name * @param exp the variable expression */ defineNegatedSet(name: string, ...exp: RegExpParam[]): RegExpCoder; /** * `x(?=y)` * Define a lookahead variable * @param name the variable name * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ defineLookahead(name: string, exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `x(?!y)` * Define a negated lookahead variable * @param name the variable name * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ defineNegatedLookahead(name: string, exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `(?<=y)x` * Define a lookbehind variable * @param name the variable name * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ defineLookbehind(name: string, exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `(?<!y)x` * Define a negated lookbehind variable * @param name the variable name * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ defineNegatedLookbehind(name: string, exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * Stash the current expression as a variable. * @param name the variable name */ stash(name: string): RegExpCoder; /** * `xy` * Append an expression * @param exp the expression */ join(...exp: RegExpParam[]): RegExpCoder; /** * `(xy)` * Append an expression for or operation * @param exp the expression */ group(...exp: RegExpParam[]): RegExpCoder; /** * `x|y` * Append an expression for or operation * @param exp the expression */ or(...exp: RegExpParam[]): RegExpCoder; /** * `[xy]` * Append an expression for set * @param exp the expression */ set(...exp: RegExpParam[]): RegExpCoder; /** * `[^xy]` * Append an expression for negated set * @param exp the expression */ negatedSet(...exp: RegExpParam[]): RegExpCoder; /** * `x(?=y)` * Append a lookahead expression * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ lookahead(exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `x(?!y)` * Append a negated lookahead expression * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ negatedLookahead(exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `(?<=y)x` * Append a lookbehind expression * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ lookbehind(exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `(?<!y)x` * Append a negated lookbehind expression. * @param exp the variable expression * @param exp2 the variable expression * @param options the build options */ negatedLookbehind(exp: string | RegExp | RegExpCoder, exp2: string | RegExp | RegExpCoder, options?: RegExpOptions): RegExpCoder; /** * `(?<name>:`.. * Begin a group * @param name the group name */ beginGroup(name?: string): RegExpCoder; /** * `)`.. * End a group * @param name the group name */ endGroup(name?: string): RegExpCoder; /** * Enable match whole */ enableMatchWhole(): RegExpCoder; /** * Clear the content * @param keepDefinitions if keep the definitions */ clear(keepDefinitions?: boolean): this; /** * Get the build result * @param flags flags */ toRegExp(flags?: string): RegExp; /** * Get source */ get source(): string; }