Visit Mozilla.org

Dokumentacja języka JavaScript 1.5:Obiekty:RegExp:lastIndex

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

[edytuj] Podsumowanie

A read/write integer property that specifies the index at which to start the next match.

Własność obiektu: RegExp
Zaimplementowana w: JavaScript 1.2, NES3.0

JavaScript 1.5: lastIndex jest własnością instancji RegExp, nie obiektu RegExp.

Wersja ECMA: ECMA-262, Edycja 3

[edytuj] Opis

lastIndex is a property of an individual regular expression object.

This property is set only if the regular expression used the "g" flag to indicate a global search. The following rules apply:

  • If lastIndex is greater than the length of the string, regexp.test and regexp.exec fail, and lastIndex is set to 0.
  • If lastIndex is equal to the length of the string and if the regular expression matches the empty string, then the regular expression matches input starting at lastIndex.
  • If lastIndex is equal to the length of the string and if the regular expression does not match the empty string, then the regular expression mismatches input, and lastIndex is reset to 0.
  • Otherwise, lastIndex is set to the next position following the most recent match.

For example, consider the following sequence of statements:

re = /(hi)?/g 
Matches the empty string.
re("hi") 
Returns ["hi", "hi"] with lastIndex equal to 2.
re("hi") 
Returns [""], an empty array whose zeroth element is the match string. In this case, the empty string because lastIndex was 2 (and still is 2) and "hi" has length 2.