ts-uf

regex

validation

isRegex

function isRegex<T>(value: CheckT<T>): value is SmartExtract<T, RegExp>;

Checks whether a value is a regex.

Example
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.

Example
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.

Example
isValidRegexFlag("g"); // true
isValidRegexFlag("gi"); // false

isInvalidRegexFlag

function isInvalidRegexFlag(flag: string): boolean;

Checks whether a character is an invalid regex flag.

Example
isValidRegexFlag("g"); // false
isValidRegexFlag("gi"); // true

areValidRegexFlags

function areValidRegexFlags(flags: string): boolean;

Checks whether a string contains only valid, non-duplicated regex flags.

An empty string is considered valid.

Example
areValidRegexFlags("g"); // true
areValidRegexFlags("gi"); // true
areValidRegexFlags("gg"); // false
areValidRegexFlags("gn"); // false

areInvalidRegexFlags

function areInvalidRegexFlags(flags: string): boolean;

Checks whether a string contains invalid or duplicated regex flags.

Example
areInvalidRegexFlags("gi"); // false
areInvalidRegexFlags("gn"); // true

hasRegexFlag

function hasRegexFlag(regexOrFlags: RegExp | string, flag: string): boolean;

Checks whether a regex or a regex flags string contains the specified flag.

Example
hasRegexFlag(/abc/gi, "g"); // true
hasRegexFlag(/abc/gi, "y"); // false
hasRegexFlag("gi", "y"); // false

lacksRegexFlag

function lacksRegexFlag(regexOrFlags: RegExp | string, flag: string): boolean;

Checks whether a regex or a regex flags string does not contain the specified flag.

Example
lacksRegexFlag(/abc/gi, "g"); // true
lacksRegexFlag(/abc/gi, "y"); // false
lacksRegexFlag("gi", "y"); // false

hasRegexFlags

function hasRegexFlags(regexOrFlags: RegExp | string, flags: string): boolean;

Checks whether a regex or a regex flags string contains all the specified flags.

Example
hasRegexFlags(/abc/gi, "gi"); // true
hasRegexFlags(/abc/gi, "gim"); // false
hasRegexFlags("gi", "gim"); // false

lacksRegexFlags

function lacksRegexFlags(regexOrFlags: RegExp | string, flags: string): boolean;

Checks whether a regex or a regex flags string contains none of the specified flags.

Example
lacksRegexFlags(/abc/gi, "ym"); // true
lacksRegexFlags(/abc/gi, "gym"); // false
lacksRegexFlags("gi", "gym"); // false

constants

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.

Example
escapeRegex("a.b+c"); // "a\\.b\\+c"

toRegexFlags

function toRegexFlags(...flagNames: RegexFlagName[]): string;

Converts regex flag names to their corresponding flags characters.

Example
toRegexpFlags("global", "multiline"); // "gm"

normalizeFlags

function normalizeFlags(flags: string): string;

Normalizes regex flags by removing duplicates and invalid flags, then sorting them.

Example
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.

Example
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.

Example
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.

Example
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.

Example
removeRegexFlags(/abc/gi, "gi"); // /abc/
removeRegexFlags("gi", "gi"); // ""

extractInvalidFlags

function extractInvalidFlags(flags: string): string;

Extract all invalid characters from a regex flags string.

On this page