คุณสมบัติLastIndex JavaScript
ตัวอย่าง
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>";
}
ความหมายและการใช้งาน
คุณสมบัติ lastIndex ระบุดัชนีที่จะเริ่มต้นการแข่งขันครั้งต่อไป
หมายเหตุ:คุณสมบัตินี้ใช้ได้เฉพาะเมื่อมีการตั้งค่าตัวปรับแต่ง "g"
คุณสมบัตินี้ส่งคืนจำนวนเต็มที่ระบุตำแหน่งอักขระทันทีหลังจากการจับคู่ล่าสุดที่พบโดยวิธี exec( ) หรือ test( )
หมายเหตุ: exec( ) และ test( ) รีเซ็ต lastIndex เป็น 0 หากไม่พบรายการที่ตรงกัน
รองรับเบราว์เซอร์
lastIndex
เป็นคุณลักษณะ ECMAScript1 (ES1)
ES1 (JavaScript 1997) ได้รับการสนับสนุนอย่างสมบูรณ์ในเบราว์เซอร์ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
RegExpObject.lastIndex
คืนมูลค่า
Type | Description |
---|---|
Number | An integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods |