isRegExp
Checks whether a value is a regular expression.
This helper performs a strict instanceof check and never throws or mutates input. It accepts both RegExp literals and RegExp objects created with new RegExp, including wrapper objects created with Object(...).
Signature
function isRegExp(value: unknown): value is RegExp
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to check. Must be a RegExp instance (including wrapper objects) to return true. |
Returns
A boolean:
true: If the value is aRegExp.false: Otherwise.
Examples
isRegExp(/abc/) // true
isRegExp(new RegExp('abc')) // true
isRegExp(Object(/abc/)) // true
isRegExp('/abc/') // false
isRegExp('abc') // false
isRegExp(null) // false
Notes
- This helper intentionally accepts
RegExpwrapper objects, because JavaScript treats them as genuineRegExpinstances. - Strings that resemble regex literals (for example,
/abc/) are not consideredRegExpvalues. - Use
normalizeRegExpif you need to convert strings intoRegExpobjects. - Use
validateRegExpif you need aResult<T>instead of a boolean.