Home > Web Front-end > HTML Tutorial > What is the role of lastIndex RegExp property in JavaScript?

What is the role of lastIndex RegExp property in JavaScript?

王林
Release: 2023-09-15 21:49:16
forward
1010 people have browsed it

lastIndex RegExp属性在JavaScript中的作用是什么?

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.

Example

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template