Java Reference
In-Depth Information
However, the match() method makes a lot more sense when you use it with regular expressions.
Then you might search for all years in the twenty‐first century—that is, those beginning with 2 .
In this case, your array would contain the values 2000 , 2000 , 2001 , and 2002 , which is much more
useful information!
regular expressions
Before you look at the split() , match() , search() , and replace() methods of the String object
again, you need to look at regular expressions and the RegExp object. Regular expressions provide
a means of defining a pattern of characters, which you can then use to split, search for, or replace
characters in a string when they fit the defined pattern.
JavaScript's regular expression syntax borrows heavily from the regular expression syntax of Perl,
another scripting language. Most modern programming languages support regular expressions, as
do lots of applications, such as WebMatrix, Sublime Text, and Dreamweaver, in which the Find
facility allows regular expressions to be used. You'll find that your regular expression knowledge
will prove useful even outside JavaScript.
Regular expressions in JavaScript are used through the RegExp object, which is a native JavaScript
object, as are String , Array , and so on. You have two ways of creating a new RegExp object. The
easier is with a regular expression literal, such as the following:
var myRegExp = /\b'|'\b/;
The forward slashes ( /) mark the start and end of the regular expression. This is a special syntax
that tells JavaScript that the code is a regular expression, much as quote marks define a string's start
and end. Don't worry about the actual expression's syntax yet (the \b'|'\b )—that is explained in
detail shortly.
Alternatively, you could use the RegExp object's constructor function RegExp() and type the following:
var myRegExp = new RegExp("\\b'|'\\b");
Either way of specifying a regular expression is fine, though the former method is a shorter, more
efficient one for JavaScript to use and therefore is generally preferred. For much of the remainder of
the chapter, you use the first method. The main reason for using the second method is that it allows
the regular expression to be determined at run time (as the code is executing and not when you are
writing the code). This is useful if, for example, you want to base the regular expression on user
input.
Once you get familiar with regular expressions, you will come back to the second way of defining
them, using the RegExp() constructor. As you can see, the syntax of regular expressions is slightly
different with the second method, so we'll return to this subject later.
Although you'll be concentrating on the use of the RegExp object as a parameter for the String
object's split() , replace() , match() , and search() methods, the RegExp object does have its own
methods and properties. For example, the test() method enables you to test to see if the string
passed to it as a parameter contains a pattern matching the one defined in the RegExp object. You
see the test() method in use in an example shortly.
 
Search WWH ::




Custom Search