Declare a Regular Expression in JavaScript Declaring regular expressions in JavaScript

JavaScript allows you to declare regular expressions in two ways: either by using the RegExp object's constructor ("new RegExp()"), and passing to it one or two string arguments. The second, shorter way is to use "literal notation", where start and end of the regular expression pattern are marked by a forward slash ("/"). The two regular expressions below are identical, and match the word "javascript", ignoring capitalization (or "case-insensitively").

// Declare regular expression with constructor
var remo1 = new RegExp("javascript","i");

// Identical regular expression in literal notation
var remo2 = /javascript/i;

Declaring Regular Expressions with the RegExp Constructor

Like other JavaScript objects, regular expressions have their own constructor. The regular expression object's name is "RegExp":

// Declare a regular expression matching a lowercase "a"
var remo = new RegExp("a");

The regular expression we declared above is in its simplest form: we simply passed the pattern to match to the RegExp constructor. Below is a variation of this script, which will match both the lowercase and uppercase "a". We passed the ignoreCase attribute (sometimes also called a "flag") as second argument.

// Declare a regular expression matching letters "a" or "A"
var remo = new RegExp("a","i");

Regular expressions' ignoreCase property will be covered later; just remember that the RegExp constructor optionally accepts a second argument.

Using the Constructor to Declare an Empty Regular Expression

JavaScript also allows you to declare a new regular expression variable without assigning any pattern or flags to it. In other words, you can declare regular expressions without passing arguments to the RegExp constructor:

// Declare an "empty" regular expression
var remo = new RegExp();

This is an empty regular expression pattern that will not match anything. Conversely, depending on JavaScript interpreters' implementation, an empty regular expression could match anything.

Using Literal Syntax to Declare Regular Expressions

In addition to the RegExp constructor, you can declare regular expressions in JavaScript by using the "literal syntax", which consists in enclosing your regular expression between two forward slashes.

// Declare a regular expression matching a lowercase "a"
var remo = /a/;

Your browsers JS interpreter automatically recognizes the letter "a" as a regular expression. Here is another regular expression declaration, matching any vowel.

// Declare a regular expression matching any vowel
var remo = /[aeiouy]/;

We will explain the meaning of square brackets later on; but it illustrates that any valid characters enclosed between forward slashes will be recognized as a regular expression by the JavaScript interpreter.

Literal Syntax and Empty Regular Expressions

You cannot declare an "empty" regular expressions with the literal syntax. You will otherwise end up with an unexpected single-line comment.

// Below: a single-line comment, not a regular expression
var remo = //;

And because JavaScript does not require semi-colon to close statements, your web browser's JS interpreter may not throw an error message. Likewise, every forward slash (among other special characters) must be "escaped" by a backslash, to avoid the same problem. Escaped regular expression matching a forward slash...