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).
An example of the literal text format is,
var re = /World/
An example of the constructor function is,
var re = new RegExp("World")
The above two statements are equivalent.
Next: Simple Usage of the Literal Text Format and the Constructor Function >>
More JavaScript Articles
More By Chrysanthus Forcha