A few questions about Regular Expressions 
Author Message
 A few questions about Regular Expressions

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?
* 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.)
* Is there a way to "reset" the RegExp so that .exec and .test will return
correct values?

String :
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklopqrstuvwxyz
RegExp : def.*o

RegExp.exec : defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklo
RegExp.test : false
String.match :
defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklo
String.search : 3
getPositions(sString, reSearch) : 3,29,55

Lars-Erik



Sun, 01 Aug 2004 20:36:05 GMT  
 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
-=-=-



Sun, 01 Aug 2004 22:42:51 GMT  
 A few questions about Regular Expressions
Netscape 4 was created before the ECMA standards. Within its own framework,
it made sense. lastIndex on RegExp was read/write.
IE 4 simply didn't implement lastIndex on the global exec.
IE 5 (WSH 5.5+) moves the functionality of lastIndex from RegExp to each
instance. This makes a lot of sense, but it is different from the spec.

Bottom line:Don't use the exec method with the global flag in client side
JavaScript!!!!
If you have WSH 5.5+, you can use global exec server-side, otherwise use the
match() method instead.

For people interested, I am attempting an article on this subject.

http://209.83.102.53/vbxml/people/bosley/regexmethod.htm

Cheers, Mark Bosley



Mon, 02 Aug 2004 02:21:32 GMT  
 A few questions about Regular Expressions

Quote:
> Netscape 4 was created before the ECMA standards. Within its own
framework,
> it made sense. lastIndex on RegExp was read/write.
> IE 4 simply didn't implement lastIndex on the global exec.
> IE 5 (WSH 5.5+) moves the functionality of lastIndex from RegExp to each
> instance. This makes a lot of sense, but it is different from the spec.

I'm just working from memory, but I thought that in JS3.0 (IE4) regular
expression objects had a lastIndex property. In JS5.0 (or maybe 5.5), it was
added to the RegExp object, but also left on regular expression objects for
backwards compatibility.

BTW, it's incorrect to qualify JScript capabilities by browser version.
Unlike Netscape, you can upgrade IE's script engines without downloading a
whole new browser. It's possible that there are users out there with an IE4
browser but a JS5.6 script engine.

Quote:
> Bottom line:Don't use the exec method with the global flag in client side
> JavaScript!!!!

You mean don't use the test method with the global flag set, don't you?
That's good advice in any script, not just client-side scripts.

The global flag is just too useful with the exec method to outright ban it.
You should, however, make sure that you use it correctly--loop until it's
exhausted.

--
A great many people mistake opinions for thoughts. -Herbert V. Prochnow

=-=-=
Steve
-=-=-



Mon, 02 Aug 2004 20:15:50 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help, really simple question w/ Regular Expressions

2. Regular Expression replace question

3. Regular Expression Question

4. Regular Expression Replace Question

5. Regular expression question

6. Regular Expression question

7. Regular Expressions Newbie Question

8. Regular expressions question

9. Regular Expression replace question

10. question about regular expression

11. complex regular expression question

12. Regular Expression question

 

 
Powered by phpBB® Forum Software