There are two ways to create a regular expression in JavaScript. You can either create a regular expression literal OR instantiate the RegExp() constructor function. Which one should you use? It depends.

Regular expression literals

To create a regular expression literal, you enclose the pattern between a pair of forward slashes, like so:

var regex = /kieran/;

As explained in the MDN Web Docs, the benefit of this approach is that:

Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.

I use these most of the time.

The RegExp() constructor function

You can also create a regular expression by instantiating the RegExp() constructor function:

var regex = new RegExp("kieran");

As explained in the MDN Web Docs:

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

Wrapping up

You should use a regular expression literal if the regular expression is constant. If it might change, or you might be getting it from another source, you should use the RegExp() constructor function.

If you're new to regular expressions, please see the following pages: