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 position immediately following the last match found by the method.
Note:if exec()
and test()
If no match is found, then lastIndex
is 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 | integer, it specifies the position of the character immediately following the last match found by the exec() or test() method. |
Technical details
The lastIndex property of the RegExp object lastIndex
The property is a readable and writable value. For regular expressions that have the g flag set, this property stores an integer that declares the position of the first character after the last match text.
The result of the last match is found by the methods RegExp.exec() and RegExp.test(), both of which use lastIndex
The position pointed to by the property is used as the starting point for the next search. This allows you to traverse all matching text 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 of the target string begins. lastIndex
property is reset to 0.
browser supports
lastIndex
เป็นคุณสมบัติของ ECMAScript1 (ES1)
ทุกบราวเซอร์เกิดการสนับสนุน ES1 (JavaScript 1997) ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
การสนับสนุน | การสนับสนุน | การสนับสนุน | การสนับสนุน | การสนับสนุน | การสนับสนุน |
- หน้าก่อน ignoreCase
- หน้าต่อไป multiline
- กลับไปยังด้านบน JavaScript RegExp 参考手册