Javascript - Regular expression creation

Introduction

A regular expression is created using pattern and flags:

Example:

Match all instances of "at" in a string globally.

var pattern1 = /at/g;

Match the first instance of "bat" or "cat", regardless of case.

var pattern2 = /[bc]at/i;

Match all three-character combinations ending with "at", regardless of case globally.

var pattern3 = /.at/gi;

The following metacharacters must be escaped when used as part of the pattern. The metacharacters are as follows:

( [ { \ ^ $ | ) ] } ? * + .

Here are some examples:

Match the first instance of "bat" or "cat", regardless of case.

var pattern1 = /[bc]at/i;

Match the first instance of "[bc]at", regardless of case.

var pattern2 = /\[bc\]at/i;

Match all three-character combinations ending with "at", regardless of case.

var pattern3 = /.at/gi;

Match all instances of ".at", regardless of case.

var pattern4 = /\.at/gi;

The preceding examples all define regular expressions using the literal form.