Java Reference
In-Depth Information
Now to fi nd all the matches, click the Match button, and this results in the screen shown in Figure 9-7.
Figure 9-7
Each match of your regular expressions found in Test Text box is put on a separate line in the Results box.
The buttons cmdTest and cmdMatch have their click events linked to the doTest() and findMatches()
functions. Let's start by looking at what happens in the doTest() function.
First, the regular expression object is created.
var testRegExp = new RegExp(document.form1.txtRegularExpression.value,
getRegExpFlags());
The fi rst parameter of the object constructor is your regular expression as contained in the
txtRegularExpression text box. This is easy enough to access, but the second parameter contains the
regular expression fl ags, and these are generated via the tick boxes in the form. To convert the tick boxes
to the correct fl ags, the function getRegExpFlags() has been created, and the return value from this
function provides the fl ags value for the regular expressions constructor. The function getRegExpFlags()
is used by both the doTest() and getMatches() functions. The getRegExpFlags() function is fairly
simple. It starts by declaring regExpFlags and setting it to an empty string.
var regExpFlags = '';
Then for each of the tick boxes, it checks to see if the tick box is ticked. If it is, the appropriate fl ag is
added to regExpFlags as shown here for the global fl ag:
if ( document.form1.chkGlobal.checked )
{
regExpFlags = 'g';
}
Search WWH ::




Custom Search