What exactly is a regular expression, and how is it handled in JavaScript? These are the questions tackled by this five-part series. In this first part, we'll introduce the topic of regular expressions (often abbreviated RegExp), and delve into objects, patterns, and variables.
Introduction to Regular Expressions in JavaScript - Regular Expression Object (Page 4 of 6 )
In the previous section, we wrote
var re = /[bcr]at/
The content "[bcr]at" is the regular expression. There is a regular expression object (or regexp object) that has the regular expression as a component (property). The regexp object has other properties and methods. We shall see these in the series.
Creating a RegExp Object
You create a RegExp object using a literal text format e.g. /[bcr]at/ , or the RegExp constructor function.
The literal text format is used as follows:
/pattern/flags
The constructor function is used as follows:
new RegExp("pattern"[, "flags"])
Note that the two forward slashes do not enclose the pattern in the constructor function. The pattern is enclosed by quotation marks. The flags are optional (more on that shortly).