Javascript new regexp

In JavaScript, you can create a new regular expression object using the RegExp constructor or the literal syntax.

Literal Syntax

const regex = /pattern/;

Where pattern is the regular expression pattern you want to match.

Constructor Syntax

const regex = new RegExp("pattern");

Where pattern is the regular expression pattern you want to match.

Options

You can also pass options to the RegExp constructor to customize the behavior of the regular expression. The options are:

For example:

const regex = new RegExp("pattern", "gi");

This regular expression will match all occurrences of the pattern in the string, ignoring case.

Properties and Methods

Regular expression objects have several properties and methods that you can use to work with the regular expression:

For example:

const regex = /hello/i;
console.log(regex.source); // outputs "hello"
console.log(regex.test("Hello World")); // outputs true
console.log(regex.exec("Hello World")); // outputs ["Hello", index: 0, input: "Hello World"]