How to use typeof with RegExp in JavaScript?

Sometimes, we want to use typeof with RegExp in JavaScript.

In this article, we’ll look at how to use typeof with RegExp in JavaScript.

How to use typeof with RegExp in JavaScript?

To use typeof with RegExp in JavaScript, we replace typeof with instanceof.

For instance, we write

const t = /^foo(bar)?$/i;
console.log(t instanceof RegExp);

to check if t is an instance of RegExp with instanceof.

t instanceof RegExp should be true since t is a regex object.

Conclusion

To use typeof with RegExp in JavaScript, we replace typeof with instanceof.