/

HTML / CSS / JavaScript Tutorial

JavaScript regular expression property: lastIndex

[this page | pdf | back links]

The lastIndex property of a JavaScript regular expression indicates the index value at which to start the next match.

 

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var str = "Hello World";
var patt = /o/gim;
patt.test(str);
var x = patt.lastIndex;
document.getElementById("Example").innerHTML =
  'var str = "Hello WORLD";' + '<br>' +
  'var patt = /o/gim;' + '<br>' +
  'patt.test(str);' + '<br>' +
  'var x = patt.lastIndex;'
document.getElementById("Result").innerHTML = x;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Regular Expressions


Desktop view | Switch to Mobile