Categories
JavaScript Answers

How to use typeof with RegExp in JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *