isRegex
Checks if a value is a regular expression.
Implementation
View Source Code
ts
/**
* Checks if the provided argument is a regular expression.
*
* @example
* ```ts
* isRegex(/abc/); // true
* isRegex(new RegExp('abc')); // true
* isRegex('abc'); // false
* isRegex(123); // false
* isRegex({}); // false
* isRegex([]); // false
* isRegex(null); // false
* isRegex(undefined); // false
* ```
*
* @param arg - The argument to be checked.
*
* @return `true` if the argument is a regular expression, otherwise `false`.
*/
export function isRegex(arg: unknown): arg is RegExp {
return arg instanceof RegExp;
}API
ts
function isRegex(value: unknown): value is RegExp;Parameters
value: The value to check
Returns
trueif the value is a RegExp,falseotherwise
Examples
Basic Usage
ts
import { isRegex } from '@vielzeug/toolkit';
isRegex(/abc/); // true
isRegex(new RegExp('abc')); // true
isRegex('abc'); // falseImplementation Notes
- Detects both literal and constructed regular expressions
- Useful for type guards and validation