JavaScript RegExp – lastIndex

JavaScript RegExp – lastIndex

In JavaScript, a regular expression is a sequence of characters that forms a search pattern. With a regular expression, you can search through data, validate input fields, and replace text.

One important aspect of regular expressions is the lastIndex property. The lastIndex property sets or returns the index at which the next search will start. It only applies to global and sticky regular expressions.

Global Regular Expression

A global regular expression is created using the g flag. With a global regular expression, you can search for all occurrences of a pattern in a string. Here is an example:

const pattern = /cat/g;
const text = "The cat caught the mouse. The cat chased the dog.";
let match;

while ((match = pattern.exec(text)) !== null) {
  console.log(`Found {match[0]} at position{match.index}.`);
  console.log(`Next search starts at position ${pattern.lastIndex}.`);
}

In this code, we declare a regular expression pattern that searches for the word “cat” using the global flag g. We also create a text string that contains two occurrences of the word “cat.”

We then use a while loop to execute the exec() method on the pattern regular expression. The exec() method finds the next match in the text string and returns an array containing information about the match.

The while loop continues until no more matches are found, and prints out the matched string and its index in the text string. It also prints out the value of the lastIndex property after each match, which represents the index at which the next search will start.

The output of this code will be:

Found cat at position 4.
Next search starts at position 12.
Found cat at position 28.
Next search starts at position 35.

This output shows that the first match is found at position 4 in the text string, and the next search will start at position 12. Similarly, the second match is found at position 28, and the next search will start at position 35.

Sticky Regular Expression

A sticky regular expression is created using the y flag. With a sticky regular expression, the search starts at the lastIndex property and only matches at the beginning of the string or the previous match position. Here is an example:

const pattern = /^cat/y;
const text = "cats are cute. cats are furry.";
let match;

while ((match = pattern.exec(text)) !== null) {
  console.log(`Found {match[0]} at position{match.index}.`);
  console.log(`Next search starts at position ${pattern.lastIndex}.`);
}

In this code, we declare a regular expression pattern that searches for the word “cat” at the beginning of the string using the sticky flag y. We also create a text string that contains two occurrences of the word “cat.”

We then use a while loop to execute the exec() method on the pattern regular expression. The exec() method finds the next match in the text string and returns an array containing information about the match.

The while loop continues until no more matches are found, and prints out the matched string and its index in the text string. It also prints out the value of the lastIndex property after each match, which represents the index at which the next search will start.

The output of this code will be:

Found cat at position 0.
Next search starts at position 3.
Found cat at position 13.
Next search starts at position 16.

This output shows that the first match is found at position 0 in the text string, and the next search will start at position 3. Similarly, the second match is found at position 13, and the next search will start at position 16.

Using lastIndex to search multiple times

You can use the lastIndex property to search for multiple occurrences of a pattern in a string without the need for a loop. Here is an example:

const pattern = /cat/g;
const text = "The cat caught the mouse. The cat chased the dog.";

const matches = text.match(pattern);
console.log(matches);
console.log(`Next search starts at position ${pattern.lastIndex}.`);

In this code, we declare a regular expression pattern that searches for the word “cat” using the global flag g. We also create a text string that contains two occurrences of the word “cat.”

We then use the match() method on the text string to find all the matches of the pattern regular expression. The match() method returns an array of all the matches found in the text string.

Finally, we print out the array of matches and the value of lastIndex, which represents the index at which the next search will start.

The output of this code will be:

[ 'cat', 'cat' ]
Next search starts at position 0.

This output shows that the match() method found two matches of the pattern regular expression in the text string, and lastIndex is now reset to 0, indicating that the search can start again from the beginning of the string.

Conclusion

In conclusion, the lastIndex property is an important aspect of global and sticky regular expressions in JavaScript. It allows you to search for all occurrences of a pattern in a string, and to control the starting position of the next search. You can use it in a loop with the exec() method, or with the match() method to find all matches in a string.

Like(0)