Pattern Matching - The RegExp Object Cont.
Workhorses - The Pattern Matching Methods
In working with the RegExp object, you'll become accustomed
to using the many methods available to both the RegExp
object, and the String object. The String object works hand-
in-hand with regular expressions to find the specific match
you're looking for. There are four JavaScript Methods within
the String object that were designed to aid in the process
of matching a pattern. They are as follows.
- match(RegExpObject) - This method searches the given string for the pattern defined within RegExpObject, which was created at an earlier point within the script.
- replace(RegExpObject, string) - This method searches the given string for a match to the RegExpObject, which was created at an earlier point within the script. If a match is found, the method replaces the matched text with string, given as a parameter to the method.
- search(RegExpObject) - This method searches the given string for a match to the RegExpObject, which was created at an earlier point within the script. When found, the method returns the character position of the starting point of the matched RegExpObject within the given searched string.
- split(RegExpObject, max - This method searches the given string for a match to the RegExpObject, which was created at an earlier point within the script. The searched string is then "split" wherever there is a match found, up to max splits. The new, splitted strings are then returned by the method within an array.
You might have noticed that all of the above String methods
required a RegExp object to function. Conversely, all of the
RegExp methods require a String object to function. The four
RegExp methods available are as follows.
- exec(stringObject) - This method searches for a pattern within stringObject, and returns the result.
- test(stringObject) - This method searches for a pattern within stringObject. If a match is found, a value of true is returned. If no matches are found, a value of false is returned.
- compile(pattern, flag - While this method doesn't use a String object, it should be mentioned here. It is used to compile a list of all of the matches found within the search string by the regular expression. The pattern is a functioning RegExp object, and the flags may be either the \g or the \i Pattern Attributes, or both.
- (stringObject) - This method behaves exactly as does the exec(stringObject) method, but in a shorthanded form.
Creating Your RegExp Objects
In the following examples of using the RegExp object, you'll
see how versatile and fairly simple the RegExp object is to
use. We'll keep things simple until you get a feel for the
Object, then up te skill level a bit. You'll be a pro by the
end of the day.
As the syntax examples earlier in this tutorial show, the
actual creation of a regular expression is quite easy.
Examine the below working example.
regExpSearch = new RegExp("(this)");
stringToSearch = "The (this) is the string to be searched.";
regExpArray = regExpSearch.exec(stringToSearch);
document.write("The last value in parentheses is : " +RegExp.lastParen);
The example first starts with the creation of a new RegExp
object, called regExpSearch. The search is for the word
"this" in brackets. Next created is the string to search
through, called stringToSearch, with the word "this" in
brackets. Next comes another variable which contains the
RegExp expression (regExpSearch) and the exec() method,
which performs the actual execution of the search. Contained
as a condition within the exec() method is the variable
representing the string to be searched, stringToSearch.
Finally a document.write statement is declared, which uses
the lastParen property of the RegExp object to return the
last value in parentheses, which is "this". The returned
value looks like the following:
The value in parentheses is : this
You can see the power you have at your fingertips when using
the RegExp object. Now for something a bit more complicated.
Examine the working example below, and study the explanation
given. var stringToSearch = new String("This is the string
of text that will be searched");
var theRegExp = /\s\w*/g;
var searchArray = stringToSearch.match(theRegExp);
if (searchArray == null) {
document.write("No matches were found");
} else {
document.write("The following matches were found within the text : <br>");
for (var n = 0; n < searchArray.length ; n++) {
document.write(searchArray[n] + "<br>");
}
}
The example is very complicated, but also very functional.
The code was started with the creation of a new string
called stringToSearch. Next came the regular expression that
was named theRegExp. After the declaration of the regular
expression, the array the results are to be put in is
created, called searchArray. The condition of the array was
executed to find a match using the match() method with
theRegExp within parentheses as the parameter. An if / else
statement was then used to write one message to the screen
if a match was found and one if no match was found. If a
match is found, then the for statement accesses the
searchArray array and displays the contents of the array,
each item on a separate line. The output of the above code
is shown below:
The following matches were found within the text :
is
the
string
of
text
that
will
be
searched
Well we've covered the RegExp object. Stop back next week where we'll look at how to utilize JavaScript on the sever-side.
Pattern Matching - The RegExp Object
The JavaScript Chronicles
JavaScript Introduction
Part 2: Data Types
Part 3: Arrays
Part 4: Operators
Part 5: Conditional Statements
Part 6: JavaScript Functions
Part 7: Pattern Matching - The RegExp Object
|