lastIndex The RegExp property allows you to call these methods repeatedly, looping over all matches in the string, and is only valid if the "g" modifier is set.
This property is read/write, so you can set it at any time to specify where in the target string the next search should start. When exec() and test() cannot find a match (or another match), they automatically reset lastIndex to 0.
You can try running the following code to use the lastIndex RegExp property in JavaScript -
<html> <head> <title>JavaScript RegExp lastIndex Property</title> </head> <body> <script> var str = "JavaScript is an interesting scripting language"; var re = new RegExp( "script", "g" ); re.test(str); document.write("Test 1 - Current Index: " + re.lastIndex); re.test(str); document.write("<br />Test 2 - Current Index: " + re.lastIndex); </script> </body> </html>
The above is the detailed content of What is the role of lastIndex RegExp property in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!