With some abstraction, you can require ` lastIndex ` to be set to a particular
value before each ` exec() ` call.

js

    
    
    function createMatcher(pattern) {
      // Create a copy, so that the original regex is never updated
      const regex = new RegExp(pattern, "g");
      return (input, offset) => {
        regex.lastIndex = offset;
        return regex.exec(input);
      };
    }
    
    const matchFoo = createMatcher(/foo/);
    console.log(matchFoo("foo bar", 0)[0]); // "foo"
    console.log(matchFoo("foo baz", 0)[0]); // "foo"
    

##  Specifications

Specification  
---  
[ ECMAScript Language Specification  
#  sec-properties-of-regexp-instances
](https://tc39.es/ecma262/multipage/text-processing.html#sec-properties-of-
regexp-instances)  
  
##  Browser compatibility

BCD tables only load in the browser  with JavaScript enabled. Enable
JavaScript to view data.

##  See also

  * [ ` RegExp.prototype.dotAll ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll)
  * [ ` RegExp.prototype.global ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global)
  * [ ` RegExp.prototype.hasIndices ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices)
  * [ ` RegExp.prototype.ignoreCase ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase)
  * [ ` RegExp.prototype.multiline ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline)
  * [ ` RegExp.prototype.source ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source)
  * [ ` RegExp.prototype.sticky ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky)
  * [ ` RegExp.prototype.unicode ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode)

##  Help improve MDN

Was this page helpful to you?

Yes  No

[ Learn how to contribute
](https://github.com/mdn/content/blob/main/CONTRIBUTING.md "This will take you
to our contribution guidelines on GitHub.") .

This page was last modified on  Sep 25, 2023  by  [ MDN contributors ](/en-
US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex/contributors.txt)
.

[ View this page on GitHub
](https://github.com/mdn/content/blob/main/files/en-
us/web/javascript/reference/global_objects/regexp/lastindex/index.md?plain=1
"Folder: en-us/web/javascript/reference/global_objects/regexp/lastindex
\(Opens in a new tab\)") â¢  [ Report a problem with this content
](https://github.com/mdn/content/issues/new?template=page-report.yml&mdn-
url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-
US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FRegExp%2FlastIndex&metadata=%3C%21--+Do+not+make+changes+below+this+line+--%3E%0A%3Cdetails%3E%0A%3Csummary%3EPage+report+details%3C%2Fsummary%3E%0A%0A*+Folder%3A+%60en-
us%2Fweb%2Fjavascript%2Freference%2Fglobal_objects%2Fregexp%2Flastindex%60%0A*+MDN+URL%3A+https%3A%2F%2Fdeveloper.mozilla.org%2Fen-
US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FGlobal_Objects%2FRegExp%2FlastIndex%0A*+GitHub+URL%3A+https%3A%2F%2Fgithub.com%2Fmdn%2Fcontent%2Fblob%2Fmain%2Ffiles%2Fen-
us%2Fweb%2Fjavascript%2Freference%2Fglobal_objects%2Fregexp%2Flastindex%2Findex.md%0A*+Last+commit%3A+https%3A%2F%2Fgithub.com%2Fmdn%2Fcontent%2Fcommit%2Ffb85334ffa4a2c88d209b1074909bee0e0abd57a%0A*+Document+last+modified%3A+2023-09-25T07%3A33%3A31.000Z%0A%0A%3C%2Fdetails%3E
"This will take you to GitHub to file a new issue.")

[ MDN logo  ](/)

Your blueprint for a better internet.

  * [ MDN on Mastodon  ](https://mozilla.social/@mdn)
  * [ MDN on X (formerly Twitter)  ](https://twitter.com/mozdevnet)
  * [ MDN on GitHub  ](https://github.com/mdn/)
  * [ MDN Blog RSS Feed  ](/en-US/blog/rss.xml)

##  MDN

  * [ About ](/en-US/about)
  * [ Blog ](/en-US/blog/)
  * [ Careers ](https://www.mozilla.org/en-US/careers/listings/?team=ProdOps)
  * [ Advertise with us ](/en-US/advertising)

##  Support

  * [ Product help ](https://support.mozilla.org/products/mdn-plus)
  * [ Report an issue ](/en-US/docs/MDN/