@adguard/agtree
Version:
Tool set for working with adblock filter lists
107 lines (106 loc) • 3.92 kB
TypeScript
/**
* @file Regular expression utilities.
*/
export declare const REGEX_START = "^";
export declare const REGEX_END = "$";
export declare const REGEX_ANY_CHARACTERS: string;
export declare const ADBLOCK_URL_START: string;
export declare const ADBLOCK_URL_START_REGEX = "^(http|https|ws|wss)://([a-z0-9-_.]+\\.)?";
export declare const ADBLOCK_URL_SEPARATOR = "^";
export declare const ADBLOCK_URL_SEPARATOR_REGEX = "([^ a-zA-Z0-9.%_-]|$)";
export declare const ADBLOCK_WILDCARD = "*";
export declare const ADBLOCK_WILDCARD_REGEX: string;
export declare const REGEX_NEGATION_PREFIX = "^((?!";
export declare const REGEX_NEGATION_SUFFIX = ").)*$";
/**
* Special RegExp symbols.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-escape
*/
export declare const SPECIAL_REGEX_SYMBOLS: Set<string>;
/**
* Utility functions for working with RegExp patterns.
*/
export declare class RegExpUtils {
/**
* Checks whether a string possibly is a RegExp pattern.
* Flags are not supported.
*
* Note: it does not perform a full validation of the pattern,
* it just checks if the string starts and ends with a slash.
*
* @param pattern Pattern to check.
*
* @returns `true` if the string is a RegExp pattern, `false` otherwise.
*/
static isRegexPattern(pattern: string): boolean;
/**
* Checks whether a string is a negated RegExp pattern.
*
* @param pattern Pattern to check.
*
* @returns `true` if the string is a negated RegExp pattern, `false` otherwise.
*/
static isNegatedRegexPattern(pattern: string): boolean;
/**
* Removes negation from a RegExp pattern.
*
* @param pattern RegExp pattern to remove negation from.
*
* @returns RegExp pattern without negation.
*/
static removeNegationFromRegexPattern(pattern: string): string;
/**
* Negates a RegExp pattern. Technically, this method wraps the pattern in `^((?!` and `).)*$`.
*
* RegExp modifiers are not supported.
*
* @param pattern Pattern to negate (can be wrapped in slashes or not).
*
* @returns Negated RegExp pattern.
*/
static negateRegexPattern(pattern: string): string;
/**
* Ensures that a pattern is wrapped in slashes.
*
* @param pattern Pattern to ensure slashes for.
*
* @returns Pattern with slashes.
*/
static ensureSlashes(pattern: string): string;
/**
* Converts a basic adblock rule pattern to a RegExp pattern. Based on
* https://github.com/AdguardTeam/tsurlfilter/blob/9b26e0b4a0e30b87690bc60f7cf377d112c3085c/packages/tsurlfilter/src/rules/simple-regex.ts#L219.
*
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#basic-rules}
*
* @param pattern Pattern to convert.
*
* @returns RegExp equivalent of the pattern.
*/
static patternToRegexp(pattern: string): string;
/**
* Creates a length-matching regular expression string: /^(?=.{min,max}$).*\/s
* Where:
* - (?=.{min,max}$) is a lookahead that ensures the string length is between min and max
* - .* matches any character (including newlines, due to the 's' flag).
*
* @param min Minimum length or `null` for no minimum (default to `0`).
* @param max Maximum length or `null` for no maximum (default to no maximum).
*
* @returns Length-matching regular expression string.
*/
static getLengthRegexp(min: number | null, max: number | null): string;
/**
* Converts a glob pattern to a RegExp string with slashes and 's' flag.
*
* @param glob Glob pattern to convert.
*
* @returns RegExp string.
*
* @example
* // Returns '/^foo.*bar$/s'
* RegExpUtils.globToRegExp('foo*bar');
*/
static globToRegExp(glob: string): string;
}