Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript lastIndex Property
The lastIndex property in JavaScript is used with regular expressions to track the position where the next search will begin. It only works with the global (g) flag and automatically updates after each match.
Syntax
regexObject.lastIndex
How lastIndex Works
The lastIndex property starts at 0 and updates to the position after each match when using exec() or test() with the global flag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript lastIndex Property</title>
</head>
<body>
<h2>Finding Multiple Matches with lastIndex</h2>
<p id="demo"></p>
<script>
let text = "The king bought an expensive ring.";
let regex = /ing/g;
let result = "";
result += "Text: " + text + "<br><br>";
result += "Search pattern: /ing/g<br><br>";
// First match
let match1 = regex.exec(text);
if (match1) {
result += "Match 1: '" + match1[0] + "' found at index " + match1.index + "<br>";
result += "lastIndex after match 1: " + regex.lastIndex + "<br><br>";
}
// Second match
let match2 = regex.exec(text);
if (match2) {
result += "Match 2: '" + match2[0] + "' found at index " + match2.index + "<br>";
result += "lastIndex after match 2: " + regex.lastIndex + "<br><br>";
}
// Third attempt (no more matches)
let match3 = regex.exec(text);
result += "Match 3: " + (match3 ? match3[0] : "null") + "<br>";
result += "lastIndex after match 3: " + regex.lastIndex;
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
Text: The king bought an expensive ring. Search pattern: /ing/g Match 1: 'ing' found at index 7 lastIndex after match 1: 10 Match 2: 'ing' found at index 31 lastIndex after match 2: 34 Match 3: null lastIndex after match 3: 0
Using test() Method
The test() method also updates lastIndex with global regex:
<!DOCTYPE html>
<html>
<body>
<p id="output"></p>
<script>
let text = "cat bat rat";
let regex = /at/g;
let output = "";
output += "Testing: " + text + "<br>";
output += "Pattern: /at/g<br><br>";
while (regex.test(text)) {
output += "Match found, lastIndex: " + regex.lastIndex + "<br>";
}
output += "No more matches, lastIndex: " + regex.lastIndex;
document.getElementById("output").innerHTML = output;
</script>
</body>
</html>
Testing: cat bat rat Pattern: /at/g Match found, lastIndex: 3 Match found, lastIndex: 7 Match found, lastIndex: 11 No more matches, lastIndex: 0
Resetting lastIndex
You can manually reset lastIndex to start searching from the beginning:
<!DOCTYPE html>
<html>
<body>
<p id="reset-demo"></p>
<script>
let text = "hello world hello";
let regex = /hello/g;
let result = "";
// First search
result += "First search:<br>";
while (regex.test(text)) {
result += "Found at lastIndex: " + regex.lastIndex + "<br>";
}
result += "<br>Resetting lastIndex to 0<br><br>";
// Reset and search again
regex.lastIndex = 0;
result += "Second search:<br>";
while (regex.test(text)) {
result += "Found at lastIndex: " + regex.lastIndex + "<br>";
}
document.getElementById("reset-demo").innerHTML = result;
</script>
</body>
</html>
First search: Found at lastIndex: 5 Found at lastIndex: 17 Resetting lastIndex to 0 Second search: Found at lastIndex: 5 Found at lastIndex: 17
Key Points
-
lastIndexonly works with the global (g) flag - It automatically resets to 0 when no more matches are found
- You can manually set
lastIndexto control where the next search begins - Without the global flag,
lastIndexis always 0
Conclusion
The lastIndex property is essential for tracking position in global regex searches. It automatically updates after each match and resets when no more matches are found, making it perfect for iterating through all occurrences in a string.
