regex
validation
isRegex
function isRegex<T>(value: CheckT<T>): value is SmartExtract<T, RegExp>;Checks whether a value is a regex.
function deleteSubstring(target: string | RegExp) {
if (isRegex(target)) {
target; // RegExp
} else {
target; // string
}
}isNotRegex
function isNotRegex<T>(value: CheckT<T>): value is Exclude<T, RegExp>;Checks whether a value is not a regex.
function deleteSubstring(target: string | RegExp) {
if (isNotRegex(target)) {
target; // string
} else {
target; // RegExp
}
}isValidRegexFlag
function isValidRegexFlag(flag: string): boolean;Checks whether a character is a valid regex flag.
isValidRegexFlag("g"); // true
isValidRegexFlag("gi"); // falseisInvalidRegexFlag
function isInvalidRegexFlag(flag: string): boolean;Checks whether a character is an invalid regex flag.
isValidRegexFlag("g"); // false
isValidRegexFlag("gi"); // trueareValidRegexFlags
function areValidRegexFlags(flags: string): boolean;Checks whether a string contains only valid, non-duplicated regex flags.
An empty string is considered valid.
areValidRegexFlags("g"); // true
areValidRegexFlags("gi"); // true
areValidRegexFlags("gg"); // false
areValidRegexFlags("gn"); // falseareInvalidRegexFlags
function areInvalidRegexFlags(flags: string): boolean;Checks whether a string contains invalid or duplicated regex flags.
areInvalidRegexFlags("gi"); // false
areInvalidRegexFlags("gn"); // truehasRegexFlag
function hasRegexFlag(regexOrFlags: RegExp | string, flag: string): boolean;Checks whether a regex or a regex flags string contains the specified flag.
hasRegexFlag(/abc/gi, "g"); // true
hasRegexFlag(/abc/gi, "y"); // false
hasRegexFlag("gi", "y"); // falselacksRegexFlag
function lacksRegexFlag(regexOrFlags: RegExp | string, flag: string): boolean;Checks whether a regex or a regex flags string does not contain the specified flag.
lacksRegexFlag(/abc/gi, "g"); // true
lacksRegexFlag(/abc/gi, "y"); // false
lacksRegexFlag("gi", "y"); // falsehasRegexFlags
function hasRegexFlags(regexOrFlags: RegExp | string, flags: string): boolean;Checks whether a regex or a regex flags string contains all the specified flags.
hasRegexFlags(/abc/gi, "gi"); // true
hasRegexFlags(/abc/gi, "gim"); // false
hasRegexFlags("gi", "gim"); // falselacksRegexFlags
function lacksRegexFlags(regexOrFlags: RegExp | string, flags: string): boolean;Checks whether a regex or a regex flags string contains none of the specified flags.
lacksRegexFlags(/abc/gi, "ym"); // true
lacksRegexFlags(/abc/gi, "gym"); // false
lacksRegexFlags("gi", "gym"); // falseconstants
TYPES
/**
* Regex flag name.
*/
export type RegexFlagName = keyof typeof regexFlag;/**
* Regex flag character.
*/
export type RegexFlag = (typeof regexFlags)[number];VARIABLES
/**
* A list of valid regex flags.
*/
export const regexFlags = ["d", "g", "i", "m", "s", "u", "v", "y"] as const;/**
* Maps regular expression flag names to their corresponding flag characters.
*/
export const regexFlag = {
hasIndices: "d",
global: "g",
ignoreCase: "i",
multiline: "m",
dotAll: "s",
unicode: "u",
unicodeSets: "v",
sticky: "y",
} as const;utils
escapeRegex
function escapeRegex(text: string): string;Escapes special regex characters so a string can be matched literally.
escapeRegex("a.b+c"); // "a\\.b\\+c"toRegexFlags
function toRegexFlags(...flagNames: RegexFlagName[]): string;Converts regex flag names to their corresponding flags characters.
toRegexpFlags("global", "multiline"); // "gm"normalizeFlags
function normalizeFlags(flags: string): string;Normalizes regex flags by removing duplicates and invalid flags, then sorting them.
normalizeFlags("iggn"); // "gi"ensureRegexFlag
function ensureRegexFlag<T extends RegExp | string>(regexOrFlags: T, flag: string): T;Ensures that a regex or regex flags string contains the specified flag.
When passed a regex, returns a new instance and preserves its lastIndex.
function findLastMatch(regex: RegExp) {
const globalRegex = ensureRegexFlag(regex, "g");
// ...
}ensureRegexFlags
function ensureRegexFlags<T extends RegExp | string>(regexOrFlags: T, flags: string): T;Ensures that a regex or regex flags string contains the specified flag.
When passed a regex, returns a new instance and preserves its lastIndex.
ensureRegexFlags(/abc/, "gm"); // /abc/gm
ensureRegexFlags("g", "gm"); // "gm"removeRegexFlag
function removeRegexFlag<T extends RegExp | string>(regexOrFlags: T, flag: string): T;Removes the specified flag from a regex or regex flags string.
removeRegexFlag(/abc/gi, "i"); // /abc/g
removeRegexFlag("gi", "i"); // "g"removeRegexFlags
function removeRegexFlags<T extends RegExp | string>(regexOrFlags: T, flags: string): T;Removes the specified flags from a regex or regex flags string.
removeRegexFlags(/abc/gi, "gi"); // /abc/
removeRegexFlags("gi", "gi"); // ""extractInvalidFlags
function extractInvalidFlags(flags: string): string;Extract all invalid characters from a regex flags string.