regexp-coder
Version:
A Javascript/Typescript RegExp Coder
230 lines (229 loc) • 7.37 kB
TypeScript
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;
/**
* `(?