Defining Regular Expressions
In JavaScript, regular expressions are represented by RegExp
objects. RegExp objects may be created with the RegExp() constructor, of course, but they
are more often created using a special literal syntax. Just as string
literals are specified as characters within quotation marks, regular
expression literals are specified as characters within a pair of slash
(/) characters. Thus, your
JavaScript code may contain lines like this:
varpattern=/s$/;
This line creates a new RegExp object and assigns it to the
variable pattern. This particular
RegExp object matches any string that ends with the letter “s.” This
regular expression could have equivalently been defined with the
RegExp() constructor like
this:
varpattern=newRegExp("s$");
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access