Categories
JavaScript Answers

How to create a case insensitive regex in JavaScript?

Spread the love

Sometimes, we want to create a case insensitive regex in JavaScript.

In this article, we’ll look at how to create a case insensitive regex in JavaScript.

How to create a case insensitive regex in JavaScript?

To create a case insensitive regex in JavaScript, we can use the i flag.

For instance, we write

const value = "some text with dAtE";
const hasDate = /date/i.test(value);

to create the /date/i regex which matches 'date' with the letters in any case.

And then we call test on it with value to match 'dAtE'.

Conclusion

To create a case insensitive regex in JavaScript, we can use the i flag.

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 *