CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isRegExp

Checks whether a value is an instance of RegExp.

isRegExp(value):value is RegExp

Behavior

The function uses the instanceof operator to determine whether the value is a RegExp instance.

RegExp Instances

Both regular expression literals and RegExp constructor instances are recognized as RegExp values.

Type Guard

The return type is value is RegExp, allowing TypeScript to narrow the value to RegExp inside a conditional branch.

Parameters

valueunknownrequired

The value to check.

Returns

value is RegExp

Returns true when the value is an instance of RegExp; otherwise, returns false.

Examples

01

Basic usage

Check whether different values are regular expressions.

TypeScript
import { isRegExp } from "@cyref/js";

isRegExp(/hello/);
// true

isRegExp(new RegExp("hello"));
// true

isRegExp("hello");
// false

isRegExp({});
// false

isRegExp(null);
// false
02

Type narrowing

isRegExp acts as a TypeScript type guard and narrows the value to RegExp when it returns true.

TypeScript
const value: unknown = /hello/i;

if (isRegExp(value)) {
  console.log(value.source);
  console.log(value.flags);
}
Built with@cyref/js·Official documentation