The regular expression is /(?!((00000)|(11111)))/ in oRe. That is
oRE=/(?!((00000)|(11111)))/
The test strings are 92708, 00000, 11111 in checkStr
The expression used is checkStr.search(oRE). The values returned are
are 0,1,1 - the values should be 0,-1,-1.
The positive lookahead expressiono RE=/(?=((00000)|(11111)))/ returns
-1, 0, 0 respectively - this is correct
The javascript used is Microsoft Javascript 1.5
Hi
I think 0, 1, 1 is correct.
Firstly, remember that ?! is non-consuming. I.e. "Lookaheads do not
consume characters, that is, after a match occurs, the search for the
next match begins immediately following the last match, not after the
characters that comprised the lookahead."
Secondly, remember that a look-ahead is testing not for a character,
but what characters follows from a given point in the string.
Conceptually, one way I like to understand a look-ahead is to consider
it as an imaginary "cursor" in the string, looking at what follows that
cursor.
Thus with your RegExp /(?!((00000)|(11111)))/ and given the string
"0000", I will use ' | ' to refer to the cursor:
1. The cursor starts at the beginning of the input: |0000
This is insertion point ' 0 '
In that case the match fails, so because ?! is non-consuming, the
cursor moves forward one position, and the test is run again:-
2. The cursor is now here: 0|000
This is insertion point ' 1 '
Here only three ' 0 ' characters that follow the cursor, so the match
succeeds.
Hence why it returns 1, because at position 1 there are only 3 zeros in
the look-ahead.
If my string were composed of 5 zeros "00000", then the match would be
at position 2 ---> 00|000
Hope this helps
Regards
Julian Turner