JavaScript RegExp lastIndex property
- Προηγούμενη σελίδα ignoreCase
- Προηγούμενη σελίδα multiline
- Επιστροφή στο προηγούμενο επίπεδο Εκπαιδευτικός Οδηγός Αναφορών JavaScript RegExp
definition and usage
lastIndex
the property specifies the index of the start of the next match.
note:this property is only valid when the "g" modifier is set.
this property returns an integer that specifies exec()
or test()
the character position immediately following the last match found by the method.
note:if exec()
and test()
If no match is found, then lastIndex
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 character position immediately following the last match found by the exec() or test() method. |
technical details
The property of the RegExp object lastIndex
the property is a readable and writable value. For regular expressions set with 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 lastIndex
the position pointed to by the property as the starting point for the next search. This allows you 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 any matching text, they will automatically set lastIndex
the property is reset to 0.
browser supports
lastIndex
Είναι χαρακτηριστικά του ECMAScript1 (ES1).
Όλοι οι περιηγητές υποστηρίζουν πλήρως το ES1 (JavaScript 1997):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Υποστήριξη | Υποστήριξη | Υποστήριξη | Υποστήριξη | Υποστήριξη | Υποστήριξη |
- Προηγούμενη σελίδα ignoreCase
- Προηγούμενη σελίδα multiline
- Επιστροφή στο προηγούμενο επίπεδο Εκπαιδευτικός Οδηγός Αναφορών JavaScript RegExp