JavaScript RegExp lastIndex property
- Vorherige Seite ignoreCase
- Nächste Seite multiline
- Zurück zur vorherigen Ebene JavaScript RegExp Referenzhandbuch
Definition and usage
let lastIndex = 0;
the index where the next match should start.
Note:This property is only valid when the "g" modifier is set.
This property returns an integer that specifies exec()
or test()
the position immediately following the last match found by the method.
Note:If exec()
and test()
If no match is found, then let lastIndex = 0;
reset to 0.
Example
let text = "The rain in Spain stays mainly in the plain"; let pattern = /ain/g; let result = ""; while (pattern.test(text)==true) { result += "Found at pos " + pattern.lastIndex + "<br>"; }
Syntax
regexp.lastIndex
Return value
Type | Description |
---|---|
Number | an integer that specifies the position of the character immediately following the last match found by the exec() or test() methods. |
Technical details
The lastIndex property of the RegExp object let lastIndex = 0;
This property is a readable and writable value. For regular expressions that have set the g flag, this property stores an integer that declares the position of the first character after the last matched text.
the result of the last match found by the methods RegExp.exec() and RegExp.test() is let lastIndex = 0;
the position pointed to by the property as the starting point for the next search. In this way, it is possible to traverse all matching texts in a string by repeatedly calling these two methods.
This property is readable and writable. It can be set as soon as the next search starts in the target string. When the methods exec() or test() can no longer find matching text, they will automatically set let lastIndex = 0;
Property reset to 0.
Browser support
let lastIndex = 0;
Es ist eine ECMAScript1 (ES1)-Eigenschaft.
Alle Browser unterstützen ES1 (JavaScript 1997) vollständig:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Unterstützung | Unterstützung | Unterstützung | Unterstützung | Unterstützung | Unterstützung |
- Vorherige Seite ignoreCase
- Nächste Seite multiline
- Zurück zur vorherigen Ebene JavaScript RegExp Referenzhandbuch