How to use a variable in Regular Expression pattern in JavaScript

In this article, we’ll show you how to use a variable in a Regular Expression pattern in JavaScript.

Here, we will refer to this article to understand the use case. In this article, we used the /is/g to replace the string. But now suppose if we have is string in the variable then we will see how to use the variable in Regular Expression.

For building a regex pattern from a dynamic string, we can use the JavaScript RegExp object.

Now if you want to replace the case insensitive string then you have to use gi flag. Checkout the below example.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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