
A few questions about Regular Expressions
Quote:
> Considering the output at the bottom of this message :
> * How come .exec returns null if placed at bottom?
> * How come .test returns false after .exec, but true before?
From the remarks section of the exec method documentation:
"If the global flag is set for a regular expression, exec searches the string
beginning at the position indicated by the value of lastIndex. If the global
flag is not set, exec ignores the value of lastIndex and searches from the
beginning of the string."
From the remarks section of the lastIndex property documentation:
"The lastIndex property is zero-based, that is, the index of the first
character is zero. It's initial value is -1. Its value is modified whenever
a successful match is made.
"The lastIndex property is modified by the exec and test methods of the
RegExp object, and the match, replace, and split methods of the String
object."
Quote:
> * How come .exec and .match doesn't return arrays of at least 3 items? (i.e
> def[1]-o[1], def[1]-o[2] etc.)
Regular expressions are greedy--they will match as many characters as they
possibly can to satisfy a pattern. Your pattern (def.*o) matches from the
first 'def' to the last 'o'.
You can modify the pattern to be lazy by adding a ? after the * and +
quantifiers. Try 'def.*?o' as your pattern.
Exec will never return an array of all the matches, you have to use it in a
loop to process all the matches:
while (re = reSearch.exec(sString)) { /* process each match */}
Quote:
> * Is there a way to "reset" the RegExp so that .exec and .test will return
> correct values?
Repeatedly test the string until the test fails. That will reset the
lastIndex property to -1.
if (reSearch.global) while (reSearch.test(sString));
The test of the regular expression's global property is necessary to prevent
a continuous loop if it's not set.
Download your own copy of the Windows Script Technologies documentation and
read up on the RegExp objects methods and properties.
http://download.microsoft.com/download/winscript56/Install/5.6/W982KM...
--
Love all, trust a few, do wrong to none. -Shakespeare
=-=-=
Steve
-=-=-