# RegExp.lastIndex

**URL:** <https://forum.freecodecamp.org/t/regexp-lastindex/448447>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 25, 2021, 7:49pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447 "2021-02-25T19:49:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuchit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yuchit/32/176231_2.png) [@yuchit](https://forum.freecodecamp.org/u/yuchit)\
**Post date:** [February 25, 2021, 7:49pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/1 "2021-02-25T19:49:12Z")

</div>

I just discovered something that I don’t quite understand when checking Reg.Exp.lastIndex.  
I don’t understand why it would produce 0 rather than 9 when it is directly applied to it without using a variable.

```auto
const regex1 = /foo/g;
const str1 = 'table football, foosball';

regex1.test(str1);

console.log(regex1.lastIndex); // output: 9
console.log(/foo/g.lastIndex); // output: 0

```

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 25, 2021, 8:00pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/2 "2021-02-25T20:00:01Z")

</div>

Because you haven’t used any methods on that RegExp object. The point of `lastIndex` is to be useful if you are repeatedly applying a regex pattern to a string. So in the example, the `test` method is used on a string. The test runs, and the `lastIndex` _for that particular instance of RegExp_ is incremented to 9. Then `test` could be ran again, now starting at that index, and so on. You haven’t done anything with the RegExp object on your last line, so the `lastIndex` is 0

---

<div class="post-metadata">

**Author:** ![yuchit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yuchit/32/176231_2.png) [@yuchit](https://forum.freecodecamp.org/u/yuchit)\
**Post date:** [February 25, 2021, 8:09pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/3 "2021-02-25T20:09:59Z")

</div>

Thanks for reply.

But please look at this below.

```auto
const regex1 = /foo/g;
const str1 = 'table football, foosball';

regex1.test(str1);
console.log(regex1.lastIndex); // output: 9

regex1.test(str1);
console.log(/foo/g.lastIndex); // output: 0

```

And if I switch to a variable for RegExp, it would produce 19 rather than 0:

```auto
const regex1 = /foo/g;
const str1 = 'table football, foosball';

regex1.test(str1);

console.log(regex1.lastIndex); // output: 9

regex1.test(str1);
console.log(regex1.lastIndex); // output: 19

```

The 2nd snippet corresponds to what you mentioned but the 1st one does not when RegExp is not stored in a variable first… I don’t understand why.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 25, 2021, 8:22pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/4 "2021-02-25T20:22:36Z")

</div>

> [@yuchit](#):
>
> `console.log(/foo/g.lastIndex)`

That’s a new RegExp object, `/foo/g`, so lastIndex is 0, it can’t be anything else: you create it then immediately check the lastIndex

`regex1` is one RegExp instance, `/foo/g` on the last line is a different one.

---

<div class="post-metadata">

**Author:** ![yuchit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yuchit/32/176231_2.png) [@yuchit](https://forum.freecodecamp.org/u/yuchit)\
**Post date:** [February 25, 2021, 8:26pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/5 "2021-02-25T20:26:03Z")

</div>

But if I just use /foo/g for all, it still does not produce the same results as with regex1:

```auto
const str1 = 'table football, foosball';

/foo/g.test(str1);
console.log(/foo/g.lastIndex); // output: 0

/foo/g.test(str1);
console.log(/foo/g.lastIndex); // output: 0

```

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 25, 2021, 8:29pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/6 "2021-02-25T20:29:17Z")

</div>

Well if I rewrite this particular example, this is exactly the same as what you’ve just written one post up, but possibly clearer w/r/t what’s confusing you:

```auto
const str1 = 'table football, foosball';

const regex1 = /foo/g;
const regex2 = /foo/g;
const regex3 = /foo/g;
const regex4 = /foo/g;

regex1.test(str1);
console.log(regex2.lastIndex);

regex3.test(str1);
console.log(regex4.lastIndex)

```

regexes 2 and 4 have nothing to do with 1 and 2, they’re different objects, similarly 4 has nothing to do with 2

---

<div class="post-metadata">

**Author:** ![yuchit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yuchit/32/176231_2.png) [@yuchit](https://forum.freecodecamp.org/u/yuchit)\
**Post date:** [February 25, 2021, 8:42pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/7 "2021-02-25T20:42:34Z")

</div>

Thanks for the example. Yes, I understand your point.  
But now I am stuck with why using /foo/g directly does not produce the same results… like the example I showed above.

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [February 25, 2021, 8:48pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/8 "2021-02-25T20:48:34Z")

</div>

Because every time you write `/foo/g`, that’s a new object, a new instance of RegExp. Every time you do it, you are doing `new RegExp("foo", "g")`: the `/foo/g` syntax is just sugar for that. Every time you construct a new RegExp object, it’s lastIndex property starts at 0. So lastIndex is only useful if you assign your newly created RegExp object to a variable then reuse it.

---

<div class="post-metadata">

**Author:** ![yuchit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/yuchit/32/176231_2.png) [@yuchit](https://forum.freecodecamp.org/u/yuchit)\
**Post date:** [February 25, 2021, 9:00pm UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/9 "2021-02-25T21:00:56Z")

</div>

Ah I see! Now I got it.  
Thank you for the explanation! 🙏

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 27, 2021, 9:01am UTC](https://forum.freecodecamp.org/t/regexp-lastindex/448447/10 "2021-08-27T09:01:19Z")

</div>

Closed on August 27, 2021, 9:01am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
