Javascript Regex LastIndex Properties

The LastIndex property in JavaScript is utilized to retrieve the final index of the matched string. By specifying the regular expression pattern, we can determine the last position before proceeding to the subsequent value. This property is particularly beneficial when working with extensive string and array data for operations and filtering. In scenarios where the string fails to match, both the exec and test methods reset the value to "0".

Syntax

This syntax is utilized to retrieve the index that comes immediately before the beginning of the subsequent value.

Example

regexObject.LastIndex;

Supported Browsers

The browsers supported by RegExp's last index properties are given below:

  • Google Chrome
  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari
  • Examples

The provided instances represent the final occurrence of the matched string.

Example 1:

In this illustration, we demonstrate the final occurrence of the matching character in relation to the lastIndex attribute. In this scenario, we apply regular expressions directly in conjunction with the lastIndex identifier.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp lastIndex properties

</title>

<style>

#demo1 {

background-color: aqua;

border: 1px solid black;

width: 370px;

}

</style>

</head>



<body>

<div id = "demo1">

<h3>

JavaScript RegExp lastIndex properties

</h3>

<h4> The lastIndex property gets the Last position of the match character of the code of the function

</h4>

<!-- Click the button to get the lastIndex properties output -->

<button onclick = "display_value();"> Click Here! </button>

<div id = "regex_lastIndex"> </div>

</div>

<script>

function display_value() {

let lastIndex_value = "java javascript data jquery js";

let lastIndex_pattern = /j/g;

var final_result = "";

while (lastIndex_pattern.test(lastIndex_value)==true)  {

//Get the last position of the matching character with the lastIndex keyword

final_result += "Available lastIndex: " + lastIndex_pattern.lastIndex + "<br>";

}

//output shows " javascript" as a lastIndex

var ele_demo = document.getElementById("regex_lastIndex");

ele_demo.innerHTML = final_result;

}

</script>

</body>

</html>

Output

The result displays the final index location of the matched string.

Example 2:

The example demonstrates the use of the lastIndex property in the array function to determine the final position of the matching character.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp lastIndex properties

</title>

<style>

#demo1 {

background-color: aqua;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp lastIndex properties

</h3>

<h4> The lastIndex property gets the Last position of the match character of the code of the function

</h4>

<!-- Click the button to get the lastIndex properties output -->

<button onclick = "display_value();"> Click Here! </button>

<div id = "regex_lastIndex"> </div>

</div>

<script>

function display_value() {

let lastIndex_value = "java, javascript data, Jquery, PHP, Js";

let lastIndex_pattern = /java/g;

var final_result = "";

while (lastIndex_pattern.test(lastIndex_value)==true)  {

//Get the last position of the matching character with the lastIndex keyword

final_result += "Available lastIndex: " + lastIndex_pattern.lastIndex + "<br>";

}

//output shows " javascript" as a lastIndex

var ele_demo = document.getElementById("regex_lastIndex");

ele_demo.innerHTML = final_result;

}

</script>

</body>

</html>

Output

The result displays the final index location of the matched string.

Example 3:

The demonstration illustrates the final position of the matched character within a string for the lastIndex attribute.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp lastIndex properties

</title>

<style>

#demo1 {

background-color: aqua;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp lastIndex properties

</h3>

<h4> The lastIndex property gets the Last position of the match character of the code of the function

</h4>

<!-- Click the button to get the lastIndex properties output -->

<button onclick = "display_value();"> Click Here! </button>

<div id = "regex_lastIndex"> </div>

</div>

<script>

function display_value() {

let lastIndex_value = "9, 81, 19, 21, 90";

let lastIndex_pattern = /9/g;

var final_result = "";

while (lastIndex_pattern.test(lastIndex_value)==true)  {

//Get the last position of the matching character with the lastIndex keyword

final_result += "Available lastIndex: " + lastIndex_pattern.lastIndex + "<br>";

}

//output shows " javascript" as a lastIndex

var ele_demo = document.getElementById("regex_lastIndex");

ele_demo.innerHTML = final_result;

}

</script>

</body>

</html>

Output

The result displays the final index location of the matched string.

Example 4:

The provided example demonstrates the function number positioned at the final occurrence of the matching character for the lastIndex property.

Example

<!DOCTYPE html>

<html>

<head>

<title>

JavaScript RegExp lastIndex properties

</title>

<style>

#demo1 {

background-color: aqua;

border: 1px solid black;

width: 370px;

}

</style>

</head>

<body>

<div id = "demo1">

<h3>

JavaScript RegExp lastIndex properties

</h3>

<h4> The lastIndex property gets the Last position of the match character of the code of the function

</h4>

<!-- Click the button to get the lastIndex properties output -->

<button onclick = "display_value();"> Click Here! </button>

<div id = "regex_lastIndex"> </div>

</div>

<script>

function display_value() {

let lastIndex_value = "8, 9, 81, 19, 21, 90";

let lastIndex_pattern = /4/g;

var final_result = "";

if(lastIndex_pattern.test(lastIndex_value)==true)  {

//Get the last position of the matching character with the lastIndex keyword

final_result += "Available lastIndex: " + lastIndex_pattern.lastIndex + "<br>";

}else{

final_result += " Not Available Available lastIndex: "

}

var ele_demo = document.getElementById("regex_lastIndex");

ele_demo.innerHTML = final_result;

}

</script>

</body>

</html>

Output

The result displays the final position index of the matched string.

Conclusion

The lastIndex property in regex displays the final position of the matched string before the subsequent characters.

Input Required

This code uses input(). Please provide values below: